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...,求出该单个数. 解法:容易想到异或的性质,两个 ...
随机推荐
- [moka同学笔记]Yii2.0 dropDownList的使用(二)
方法一: <?php $psObjs = Poststatus::find()->all(); $allStatus = ArrayHelper::map($psObjs,'id','na ...
- StackOverflow Update: 560M Pageviews A Month, 25 Servers, And It's All About Performance
http://highscalability.com/blog/2014/7/21/stackoverflow-update-560m-pageviews-a-month-25-servers-and ...
- Sharepoint学习笔记—习题系列--70-573习题解析 -(Q66-Q69)
Question 66You have a custom theme named MyTheme. The theme is defined in a file named MyTheme.thmx. ...
- 【转】深入浅出Android Support Annotation
[转自]http://jcodecraeer.com/a/anzhuokaifa/androidkaifa/2015/0427/2797.html http://www.flysnow.org/201 ...
- Iterator 迭代器(一)
迭代器(iterator)是一种对象,它能够用来遍历标准模板库容器中的部分或全部元素,每个迭代器对象代表容器中的确定的地址.迭代器修改了常规指针的接口,所谓迭代器是一种概念上的抽象:那些行为 ...
- IOS编程思想
从今天起想走进IOS架构的大门,一直不屑于学习第三方框架,觉得框架也是一点点代码给垒起来的,只要掌握了代码就可以了,殊不知垒代码的过程才是最重要的,而这个过程又岂是一朝一夕就能达到完美境界的,达到完美 ...
- AFNetworking使用方法
官网下载2.5版本:http://afnetworking.com/ 此文章是基于AFNetworking2.5版本的,需要看AFNetworking2.0版本的请看上一篇文章:AFNetworkin ...
- Swift语言
本文为熟悉使用Objective-C开发iOS快速转入swift开发提供一些便利的途径,没有过多华丽的说明底层原理,也没有过多的概念说明,只提供swift的基本使用以及一些使用示例.高手请让路哈. S ...
- Java 参数传递都是值传递
Java 参数传递都是值传递,验证代码如下 public class ParamTransferTest { public static void swap(int a, int b) { int t ...
- 基于git的工作流程
本文针对的是追求极致.快速的产品响应团队的.以下的观点和内容都是围绕这个主题,暂时不涉及个人学习和团队学习. 在说工作流程之间,想说一下我们平常工作中遇到的一些困惑或者说现象 在一个团队里,同时有好多 ...