题目:

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:

  1. The order of the result is not important. So in the above example, [5, 3] is also correct.
  2. Your algorithm should run in linear runtime complexity. Could you implement it using only constant space complexity?

链接: http://leetcode.com/problems/single-number-iii/

题解:

昨晚卡到现在。但昨晚睡觉前google了一下,看到了几篇文章以后才理解。还是需要加强bit manipulation。 归根结底是大学时数字电路没学好 -_____-!! 我不堪的本科生涯...略去一亿字。

这里我们主要是先异或所有数字,得到 a^b, 因为a和b不相等,所以 a^b里至少有一个bit位k,k = 1。 然后我们就可以用这个第k位来进行判断, 因为a和b里只有一个数第k位 = 1,我们对输入数组所有第k位为1的数字进行异或,就可以找到第k位为1的仅出现一次的数字, 再用这个数字a和 a^b进行异或,我们也就求得了b。

Time Complexity - O(n), Space Complexity - O(1)。

public class Solution {public int[] singleNumber(int[] nums) {int[] res = {-1, -1};if(nums == null || nums.length == 0) {return res;}int a_XOR_b = 0;  for(int i = 0; i < nums.length; i++) {      // find a ^ ba_XOR_b ^= nums[i];}int k = 0;                              // find one bit in a^b == 1for(int i = 0; i < 31; i++) {           // that means only one num in a, b has 1 on kth bitif(((a_XOR_b >> i) & 1) == 1) {k = i;break;}}int a = 0;for(int i = 0; i < nums.length; i++) {   // since only one num in a, b has 1 on kth bitif(((nums[i] >> k) & 1) == 1) {      // we can XOR all nums has 1 on kth bita ^= nums[i];                    // duplicate nums will be evened out
            }}int b = a ^ a_XOR_b;res[0] = a;res[1] = b;return res;}
}

二刷:

也是用一刷一样的方法,比较tricky

Java:

public class Solution {public int[] singleNumber(int[] nums) {int[] res = {-1, -1};if (nums == null || nums.length == 0) {return res;}int a_XOR_b = 0;for (int i : nums) {a_XOR_b ^= i;}int k = 0;for (int i = 0; i < 32; i++) {if (((a_XOR_b >> i) & 1) == 1) {k = i;break;}}res[0] = 0;for (int i : nums) {if (((i >> k) & 1) == 1) {res[0] ^= i;}}res[1] = a_XOR_b ^ res[0];return res;}
}

题外话:

其实本科时,学校开设的课程挺不错的,老师也努力务实,都怪自己没把心思放在学习上。虽然专业是EE,但像什么基数排序,霍夫曼编码,游程码,最短路径之类的东西,老师们也都多少介绍过。信息量很大,作业和考试也难。真要四年扎扎实实学下来的话,也不至于现在一把年纪了还在刷题。 过去的都过去了,接下来好好努力吧。

三刷:

这次就比较熟练了,尝试一些不同的编程风格。  4/4/2016。

Java:

Time Complexity - O(n), Space Complexity - O(1)。

public class Solution {public int[] singleNumber(int[] nums) {int[] res = new int[2];if (nums == null || nums.length < 2) return res;int a_xor_b = 0;for (int num : nums) { a_xor_b ^= num; }int diffIndex = 0;while (diffIndex < 32) {if (((a_xor_b >> diffIndex) & 1) == 1) { break; }diffIndex++;}int num1 = 0;for (int num : nums) {if (((num >> diffIndex) & 1) == 1) { num1 ^= num; }}res[0] = num1;res[1] = a_xor_b ^ num1;return res;}
}

Update:

public class Solution {public int[] singleNumber(int[] nums) {if (nums == null || nums.length < 2) return new int[] {-1, -1};int a_XOR_b = 0;for (int num : nums) a_XOR_b ^= num;int k = 0;while (k < 31) {if (((a_XOR_b >> k) & 1) == 1) break;k++;}int a = 0;for (int num : nums) {if (((num >> k) & 1) == 1) a ^= num;}return new int[] {a, a ^ a_XOR_b};}
}

Reference:

https://groups.google.com/forum/#!topic/pongba/9KCA7b484OE

https://groups.google.com/forum/#!topic/pongba/drV6dytcoJE

http://www.matrix67.com/blog/archives/511

260. Single Number III相关推荐

  1. 【?异或】LeetCode 260. Single Number III

    LeetCode 260. Single Number III Solution1: 博客转载自:http://www.cnblogs.com/grandyang/p/4741122.html 这道题 ...

  2. leetcode 算法解析(一):260. Single Number III(C++版本和自己的注解)

    这个题来自<剑指offer>但是书上上感觉讲解不太详细,还是看博客吧(我把下面博客改写成了C++版本运行通过) 注意这个题的相关代码中,输入的数组只能有两个数出现一次,如果有第三个数出现一 ...

  3. leetcode 260. Single Number III | 260. 只出现一次的数字 III(位运算:分组异或)

    题目 https://leetcode.com/problems/single-number-iii/ 题解:分组异或 参考1:讨论区题解 you know you can eliminate dou ...

  4. 【LeetCode】-- 260. Single Number III

    问题描述: https://leetcode.com/problems/single-number-iii/ 在一个数组里面,只有两个元素仅出现过1次,其余都出现过两次.找出出现仅一次的那两个(a, ...

  5. Leet Code OJ 260. Single Number III [Difficulty: Medium]

    题目: Given an array of numbers nums, in which exactly two elements appear only once and all the other ...

  6. LeetCode 260. Single Number III

    转载请注明出处:http://www.cnblogs.com/liangyongrui/p/6354552.html 异或的妙用. 一开始读题不仔细,以为有很多的孤立数字. 没想到就两个- - 然后参 ...

  7. LeetCode Single Number III(位操作)

    问题:给出一个数组,有两个数只出现一次,其它都出现两次. 思路:先对数组求异或,得到两个数异域的结果.然后确定差异的最低位.再将数组与这个差异位作异域,得到其中的一个数.再与异域结果异域后即得到另外一 ...

  8. lintcode 中等题:Single number III 落单的数III

    题目 落单的数 III 给出2*n + 2个的数字,除其中两个数字之外其他每个数字均出现两次,找到这两个数字. 样例 给出 [1,2,2,3,4,4,5,3],返回 1和5 挑战 O(n)时间复杂度, ...

  9. 【leetcode79】Single Number III

    题目描述: 给定一个数组,里面只有两个数组,只是出现一次,其余的数字都是出现两次,找出这个两个数字,数组形式输出 原文描述: Given an array of numbers nums, in wh ...

最新文章

  1. Javaweb环境配置,使用2014MyEclipse全过程详解!搭建JDK环境,Tomcat环境!破解2014MyEclipse。
  2. 工作总结 -- 插件篇 目录
  3. P3225 [HNOI2012]矿场搭建
  4. SoringMVC-常用注解标签详解(摘抄)
  5. Python中的四种交换数值的方法
  6. 命令行下一种新的加帐号的方法
  7. php小于neq qe,PHP模板判断语句eq相等 ne、neq不相等, gt大于, lt小于
  8. workbook加载文件路径_【Python】文件重命名(按照Excel清单)
  9. 右键文件夹电脑卡死?
  10. java mysql dump_mysql dump备份和恢复
  11. java两天速成_JAVA速成
  12. Win10外接显示频黑屏解决
  13. css 去掉a标签下划线,CSS 解决 a标签去掉下划线 text-decoration: none无效 的解决方案...
  14. QQ空间说说批量删除
  15. 分布式主动感知在智能运维中的实践
  16. python根据坐标画点并标记_python-如何使用colormap为matplotlib散点图中的特定点设置标记类型...
  17. 爬取《电影天堂》,保存评分大于7.0 的电影地址
  18. .shape()与.reshape()函数
  19. python用logging模块写循环日志
  20. 资源分享 | 仅需一个微软账号即可每天白嫖两小时Microsoft学习实验虚拟机云电脑...

热门文章

  1. ITK:将矢量图像投射为另一种类型
  2. DCMTK:CT位置FG类测试
  3. VTK:可视化之ChooseTextColor
  4. VTK:Rendering之FlatVersusGouraud
  5. C++链表linked list(附完整源码)
  6. C语言 内存管理之栈
  7. QT的QMediaPlaylist类的使用
  8. java类似goto_原来java中也有类似goto语句的标签啊--java label标签
  9. Rational Rose正逆向工程(类图转Java代码,Java代码转类图)
  10. 5.中文问题(自身,操作系统级别,应用软件的本身),mysql数据库备份