问题描述:

  https://leetcode.com/problems/single-number-iii/

  在一个数组里面,只有两个元素仅出现过1次,其余都出现过两次。找出出现仅一次的那两个(a, b)。

要求常量空间,线性时间。

问题解决:

  这题用到“神奇的位运算”。

  1.因为除了特殊的两个元素,其余两两出现,那么就想到了XOR(异或)。

  2.从头到尾XOR之后,会得到a xor b 的结果。接下来我们试着把这两个元素分离开来。

  3.我们在a xor b 中找到任意一位XOR[diff_pos] = 1 , 那么可知在diff_pos这位上a 和 b 是不一样的。如果按照diff_pos这位的值

  分类可以将所有数分成两组: 1)diff_pos = 0的元素, 2)diff_pos = 1的元素。

  4.对3中的两组分别组内xor,因为其余元素都是 两两出现,那么最后就剩下a / b 了。

代码如下:

class Solution(object):
def singleNumber(self, nums):
xor = 0
for num in nums:
xor = xor^num
diff_pos = 0
for i in range(31):
if(xor & (1 << i)):
diff_pos = i
break
rec = [0,0]
for num in nums:
if(num & (1 << diff_pos)):
rec[1] ^= num
else:
rec[0] ^= num
return rec

  论文还没看,又在瞎搞了。。。

找最右边的1比特位确实有更好的方法:

xor = xor & ~(xor - );

【LeetCode】-- 260. Single Number III的更多相关文章

  1. 【LeetCode】260. Single Number III 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 异或 字典 日期 题目地址:https://leet ...

  2. 【leetcode】260. Single Number III

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

  3. 【一天一道LeetCode】#260. Single Number III

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  4. 【LeetCode】137. Single Number II 解题报告(Python)

    [LeetCode]137. Single Number II 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/single- ...

  5. 【刷题-LeeetCode】260. Single Number III

    Single Number III Given an array of numbers nums, in which exactly two elements appear only once and ...

  6. 【LeetCode】136. Single Number 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 异或 字典 日期 [LeetCode] 题目地址:h ...

  7. 【LeetCode】137. Single Number II (3 solutions)

    Single Number II Given an array of integers, every element appears threetimes except for one. Find t ...

  8. 【LeetCode】136. Single Number (4 solutions)

    Single Number Given an array of integers, every element appears twice except for one. Find that sing ...

  9. 【LeetCode】137. Single Number II

    题目: Given an array of integers, every element appears three times except for one. Find that single o ...

随机推荐

  1. SVM简单上手示例

    转载自 百度知道 id:风_南(https://zhidao.baidu.com/usercenter?uid=e9904069236f25705e799313) 转载只为方便学习复习,侵删. 在用s ...

  2. 【BZOJ 1013】球形空间产生器sphere(高斯消元)

    球形空间产生器sphere HYSBZ - 1013 (高斯消元) 原题地址 题意 给出n维的球上的n个点,问原球体球心. 提示 n维球体上两点距离公式\(dist = \sqrt{ (a1-b1)^ ...

  3. [Codeforces 876]比赛记录

    上场$rating$果然炸飞,但是据说这次只要不$FST$就能翻回来QWQ? T1  $dfs$乱搞? T2  取模乱搞,$STL$ $vector$大法好(%%%$ryf$秒出做法) T3  看了半 ...

  4. noip模拟赛 立方数

    题目描述LYK定义了一个数叫“立方数”,若一个数可以被写作是一个正整数的3次方,则这个数就是立方数,例如1,8,27就是最小的3个立方数.现在给定一个数P,LYK想要知道这个数是不是立方数.当然你有可 ...

  5. Drools介绍与使用

    Drools 是用 Java 语言编写的开放源码规则引擎,使用 Rete 算法对所编写的规则求值.Drools 允许使用声明方式表达业务逻辑.可以使用非 XML 的本地语言编写规则,从而便于学习和理解 ...

  6. Ubuntu 16.04清楚Dash历史记录

    1.[系统设置]->[安全和隐私]->[文件和应用]->[清除使用数据] 2.清楚播放记录 rm -v ~/.local/share/recently-used.xbel 3.清楚打 ...

  7. MyBatis 3判断不为null

    <if test="type!=null and type!=''"> AND type = #{type} </if>

  8. maven bug之Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.5.1:compile (default-compile) on project acSpaceCommon: Fatal error compiling: tools.jar not found: C:\Program Files\J

    maven打包项目的时候一直报这个异常  一般的解决办法我都试过 在pom.xml加代码 也不行  只有10分了  求大神解答 这是因为测试代码时遇到错误,它会停止编译.只需要在pom.xml的< ...

  9. symfony2笔记

    路由可以在全局定义,也可以在单个bundle内部定义 全局定义:app/config/routing.yml 局部bundle定义:src/Miyaye/webBundle/Resources/con ...

  10. python字符串截取

    python字符串截取 str = 'abcd' str[0:-1],-1表示末尾开始的位置,但是[]操作符不取尾下标所对应的字符: 所以str[0:-1] ---->‘abc' str[0:1 ...