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...,求出该单个数. 解法:容易想到异或的性质,两个 ...
随机推荐
- Ansible用于网络设备管理 part 3 使用NAPALM成品库
闲话 经过了这俩月的闲暇时间的瞎逛和瞎琢磨,我发现NAPALM是一条路,NAPALM是由帅哥David Barroso和美女Elisa Jasinska创建的一个项目,都是颜值高的技术牛人啊,真是不给 ...
- Bootstrap 我的学习记录4 轮播图的使用和理解
<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="utf-8& ...
- linux常识以及常用命令和参数
linux,it人士众所周知,一款稳定.强大.开源的系统,1973年,unix正式诞生,ritchie等人用c语言写出第一个unix内核,之后经过不后人不断的改进,形成现在linux的各个版本,其中比 ...
- CLEAR REFRESH FEEE的区别
clear,refresh,free都有用来清空内表的作用,但用法还是有区别的.clear itab,清空内表行以及工作区,但保存内存区.clear itab[],清空内表行,但不清空工作区,但保存内 ...
- Centos 7: 打开Samba防火墙端口
firewall-cmd --permanent --add-port=137/tcp firewall-cmd --permanent --add-port=138/tcp firewall-cmd ...
- 自定义控件--CircleImageView(类似于QQ、微信圆形头像自定义控件)
现在基本上所有的需要用户注册的APP都有一个需要用户上传头像的需求,上传的头像基本都是类似于QQ.微信等社交应用圆形头像.最近,正在做的一个社交应用多处需要用到这种圆形头像的处理,总不能每次都对图片做 ...
- iOS-申请测试证书详解(多图原创)
申请测试证书详解 前言 App开发和发布过程中证书基础知识:1. Certification(证书)证书是对电脑开发资格的认证,每个开发者帐号有一套,分为两种:1) Developer Certifi ...
- iOS之 开发中用得到的开源github
github:无限图片轮播 https://github.com/dymx101/DYMRollingBanner 2.灌水动画 https://github.com/dsxNiubility/SXW ...
- iOS label根据显示内容自适应大小
- (void)setupLabel { //准备工作 UILabel *textLabel = [[UILabel alloc] init]; textLabel.font = [UIFont sy ...
- 多线程基础(三)NSThread基础
3.多线程基础 NSThread的基本使用 如何使用NSThread创建新线程 创建线程之后是默认不执行的状态 创建三个线程: 通过name属性区别这些线程 接下来就是通过设置线程的优先级来设 ...