leetcode-Single Number III 找独数
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。恰恰我们可以利用这一点,将两者分开!我们假设这个比特位是第k位。
此时,我们可以再次遍历数组,将元素中第k比特位为0和1的分开成两队单独进行异或运算。这样一来,我们不仅保证了两队中都会有一个最后的独数,而且还确保了这两个队列中有且仅有这个数是单独的,其余元素仍然是成对出现。所以,这两组队列分别进行异或运算之后的结果就是我们想要的答案。解题成功。
class Solution
{
public:
vector<int> singleNumber(vector<int>& nums)
{
// Pass 1 :
// Get the XOR of the two numbers we need to find
int diff = accumulate(nums.begin(), nums.end(), 0, bit_xor<int>());
// Get its last set bit
diff &= -diff; // Pass 2 :
vector<int> rets = {0, 0}; // this vector stores the two numbers we will return
for (int num : nums)
{
if (num & diff) // the bit is set
{
rets[0] ^= num;
}
else // the bit is not set
{
rets[1] ^= num;
}
}
return rets;
}
};
但可悲的是,我使用的是python语言,要想拿到某个数字的比特位是可以,使用bin()函数就行,但是该函数返回的字符串却是长度不一的!这让我在取某位比特位的时候经常碰到index out of range这样的错误。还是有点淡淡地伤感的,不过没关系,我们通过哈希表依然能快速的得到答案。
>>> bin(1) #长度不一好痛苦。。
'0b1'
>>> bin(2)
'0b10'
>>> bin(9)
'0b1001'
leetcode-Single Number III 找独数的更多相关文章
- [Leetcode] single number ii 找单个数
Given an array of integers, every element appears three times except for one. Find that single one. ...
- [LeetCode] Single Number III 单独的数字之三
Given an array of numbers nums, in which exactly two elements appear only once and all the other ele ...
- LeetCode Single Number III
原题链接在这里:https://leetcode.com/problems/single-number-iii/ 题目: Given an array of numbers nums, in whic ...
- [LeetCode] Single Number III ( a New Questions Added today)
Given an array of numbers nums, in which exactly two elements appear only once and all the other ele ...
- LeetCode——Single Number(找出数组中只出现一次的数)
问题: Given an array of integers, every element appears twice except for one. Find that single one. No ...
- 137 Single Number II(找唯一数Medium)
题目意思:一个int数组,有一个数只出现一次,其他数均出现三次,找到这个唯一数 思路: 1.将所有数用2进制表示,计算每一位的数字和 1*3*n1+0*3*n2+c 唯一数对应位的数字(0或者1 ...
- LeetCode——Single Number III
Description: Given an array of numbers nums, in which exactly two elements appear only once and all ...
- LeetCode Single Number III (xor)
题意: 给一个数组,其中仅有两个元素是出现1次的,且其他元素均出现2次.求这两个特殊的元素? 思路: 跟查找单个特殊的那道题是差不多的,只是这次出现了两个特殊的.将数组扫一遍求全部元素的异或和 x,结 ...
- LeetCode Single Number I / II / III
[1]LeetCode 136 Single Number 题意:奇数个数,其中除了一个数只出现一次外,其他数都是成对出现,比如1,2,2,3,3...,求出该单个数. 解法:容易想到异或的性质,两个 ...
随机推荐
- Backbone学习笔记一Backbone中的MVC
原文章地址http://bigdots.github.io/2015/12/01/Backbone学习笔记(一)/#more Backbone.js为复杂WEB应用程序提供模型(models).集合( ...
- R语言学习笔记:向量
向量是R语言最基本的数据类型. 单个数值(标量)其实没有单独的数据类型,它只不过是只有一个元素的向量. x <- c(1, 2, 4, 9) x <- c(x[1:3], 88, x[4] ...
- 【读书笔记】iOS-Xcode知识-多线程
一,Xcode使用的调试器是GDB.GDB是GNU项目的一部分,它可以在很多不同的平台上使用.如果你愿意,可以通过命令行来运行它.GDB有着完善的文档系统,尽管它的文档有些难于理解并且网络上流传着好几 ...
- Eclipse环境下配置spket中ExtJS5.0提示
使用eclipse编写extjs时,一定会用到spket这个插件,spket可以单独当作ide使用,也可以当作eclipse插件使用,我这里是当作eclipse的插件使用的,下面来一步步图解说明如何配 ...
- mod_slotmem mod_manager mod_proxy_cluster mod_advertise Permission denied
restorecon /etc/httpd/modules/mod_slotmem.so
- android 显示意图
//显示意图 public void enter(View view) { Intent intent = new Intent();//创建一个空的意图 intent.setClassName(ge ...
- java 之 对象与垃圾回收
1.垃圾回收机制只负责回收堆内存中的对象,不会回收任何物理资源(如数据库连接,网络IO等资源) 2.程序无法精确控制垃圾回收的运行,垃圾回收会在合适的时候运行.当对象永久的失去引用后,系统会在合适的时 ...
- ubuntu 获取root权限
实验环境: ubuntu 13.04 背景:现在有一台装有 ubuntu 的电脑,如何获取root权限? 方案一:进入单用户维护模式,重置root密码. 方案二:U盘挂载原根分区,修改/etc/pas ...
- 2.1.12 Next Permutation 下一个字典序数组
对当前排列从后向前扫描,找到一对为升序的相邻元素,记为i和j(i < j).如果不存在这样一对为升序的相邻元素,则所有排列均已找到,算法结束:否则,重新对当前排列从后向前扫描,找到第一个大于i的 ...
- leveldb源码分析—Recover和Repair
leveldb作为一个KV存储引擎将数据持久化到磁盘,而对于一个存储引擎来说在存储过程中因为一些其他原因导致程序down掉甚至数据文件被破坏等都会导致程序不能按正常流程再次启动.那么遇到这些状况以后如 ...