Single Number III——LeetCode
Given an array of numbers nums, in which exactly two elements appear only once and all the other elements appear exactly twice. Find the two elements that appear only once.
For example:
Given nums = [1, 2, 1, 3, 2, 5], return [3, 5].
Note:
- The order of the result is not important. So in the above example,
[5, 3]is also correct. - Your algorithm should run in linear runtime complexity. Could you implement it using only constant space complexity?
题目大意:一个数组,有两个元素出现了一次,其余元素都出现两次,让用线性时间复杂度和常量空间复杂度找出这两个数。
解题思路:考虑位操作,对所有元素做异或操作,那么最后得到的值就是要求的两个元素异或得到的,那么找出其中为1的某一位,说明这一位两个数一个为0,一个为1,以这一位为标准,把数组的元素分为两组A、B,那么要求的两个元素肯定是一个在A组,一个在B组,而对这两组各自做异或操作,就可以得到两个数就是要求的。
public class SingleNumberIII {
public static void main(String[] args) {
int[] res = singleNumber(new int[]{1, 2, 1, 3, 5, 2});
System.out.println(Arrays.toString(res));
}
public static int[] singleNumber(int[] nums) {
int[] res = new int[2];
if (nums == null || nums.length == 0) {
return res;
}
for (int i = 0; i < nums.length; i++) {
res[0] ^= nums[i];
}
int pos = 0;
for (int i = 0; i < 32; i++) {
int offset = 1 << i;
if ((res[0] & (offset)) == offset) {
pos = i;
break;
}
}
res[0] = 0;
int mask = 1 << pos;
for (int i = 0; i < nums.length; i++) {
if ((mask & nums[i]) == mask) {
res[0] ^= nums[i];
} else {
res[1] ^= nums[i];
}
}
return res;
}
}
Single Number III——LeetCode的更多相关文章
- Single Number III leetcode java
问题描述: Given an array of numbers nums, in which exactly two elements appear only once and all the oth ...
- Single Number III - LeetCode
Given an array of numbers nums, in which exactly two elements appear only once and all the other ele ...
- leetcode 136 Single Number, 260 Single Number III
leetcode 136. Single Number Given an array of integers, every element appears twice except for one. ...
- LeetCode 260. Single Number III(只出现一次的数字 III)
LeetCode 260. Single Number III(只出现一次的数字 III)
- leetcode 136. Single Number 、 137. Single Number II 、 260. Single Number III(剑指offer40 数组中只出现一次的数字)
136. Single Number 除了一个数字,其他数字都出现了两遍. 用亦或解决,亦或的特点:1.相同的数结果为0,不同的数结果为1 2.与自己亦或为0,与0亦或为原来的数 class Solu ...
- Single Number III(LintCode)
Single Number III Given 2*n + 2 numbers, every numbers occurs twice except two, find them. Example G ...
- LeetCode136 Single Number, LeetCode137 Single Number II, LeetCode260 Single Number III
136. Single Number Given an array of integers, every element appears twice except for one. Find that ...
- 【刷题-LeeetCode】260. Single Number III
Single Number III Given an array of numbers nums, in which exactly two elements appear only once and ...
- [LeetCode] Single Number III 单独的数字之三
Given an array of numbers nums, in which exactly two elements appear only once and all the other ele ...
随机推荐
- Springmvc中@RequestParam传值中文乱码解决方案(转)
@RequestMapping(value={"/list"},method=RequestMethod.GET) @ResponseBody public DeviceList ...
- CSS3伪类nth-child结合transiton动画实现文字若影若现
css3伪类nth-child结合transiton动画实现文字若影若现收先创建一个div盒子,然后包裹在div中的有10个span标签每个span标签填上内容一次为A,B,C,D,E,F,G,H,I ...
- TOM大师脚本01-查找未建索引的外键
[oracle@Oracle11g 2016]$ cat 022201.sql column columns format a30 word_wrappedcolumn tablename forma ...
- idea配置tomcat.md
[toc] 1.打开Edit Configurations,可以通过万能搜索快速进入!!! 2.添加服务器,在左上角找到Tomcat并添加 3.配置发布路径,Server标签页中填写完名称和路径,在D ...
- IOS DLNA PlatinumKit库的使用
前段时间进行了IOS DLNA的开发,使用的是PlatinumKit库.网上查了很多资料都未果,经过自己的摸索,遂将如何使用PlatinumKit进行DLNA的开发分享给大家. 1.PlatinumK ...
- jQuery慢慢啃之文档处理(五)
1.append(content|fn)//向每个匹配的元素内部追加内容. $("p").append("<b>Hello</b>"); ...
- 『重构--改善既有代码的设计』读书笔记----Self Encapsulate Field
如果你直接访问一个字段,你就会和这个字段直接的耦合关系变得笨拙.也就是说当这个字段权限更改,或者名称更改之后你的客户端代码都需要做相应的改变,此时你可以为这个字段建立设值和取值函数并且只以这些函数来访 ...
- js获取屏幕(设备)宽高
平常获取设备的宽高无非就那几 <script language="javascript"> var h = ""; h += " 网页可见 ...
- CentOS 6.3 卷组挂载硬盘教程 linux的VPS如何分区
XEN架构VPS提供的容量一般都不会低于10G,但大部分基于Xensystem面板的VPS默认挂载10G硬盘(第一磁盘),剩下的容量(第二磁盘)就需要通过手动挂载才能扩充默认的10G容量了.默认装完系 ...
- github克隆项目中的子模块submodule时遇到的问题
GitHub真是个开源大宝库,不只能学习代码,还能学习git的使用! 最近在研究Off-the-Record-iOS项目(https://github.com/chrisballinger/Off-t ...