Single Number III
Description:
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?
Code:
vector<int> singleNumber(vector<int>& nums) {
vector<int>result;
if (nums.size() < )
return result;
int xorAll = ;
for (int i = ; i < nums.size(); ++i)
xorAll^=nums[i];
unsigned int x = ;
while ((xorAll & x) == )
{
x=x<<;
}
int result1=,result2=;
for (int j = ; j < nums.size(); ++j)
{
if ((nums[j]&x) == )
result1^=nums[j];
else
result2^=nums[j];
}
result.push_back(result1);
result.push_back(result2);
return result;
}
PS:
思路很清晰,但是写代码的是后老在细节出错,主要是两个判断条件要注意
Single Number III的更多相关文章
- leetcode 136. Single Number 、 137. Single Number II 、 260. Single Number III(剑指offer40 数组中只出现一次的数字)
136. Single Number 除了一个数字,其他数字都出现了两遍. 用亦或解决,亦或的特点:1.相同的数结果为0,不同的数结果为1 2.与自己亦或为0,与0亦或为原来的数 class Solu ...
- leetcode 136 Single Number, 260 Single Number III
leetcode 136. Single Number Given an array of integers, every element appears twice except for one. ...
- LeetCode 260. Single Number III(只出现一次的数字 III)
LeetCode 260. Single Number III(只出现一次的数字 III)
- Single Number III(LintCode)
Single Number III Given 2*n + 2 numbers, every numbers occurs twice except two, find them. Example G ...
- LeetCode136 Single Number, LeetCode137 Single Number II, LeetCode260 Single Number III
136. Single Number Given an array of integers, every element appears twice except for one. Find that ...
- 【刷题-LeeetCode】260. Single Number III
Single Number III Given an array of numbers nums, in which exactly two elements appear only once and ...
- [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】#260. Single Number III
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
随机推荐
- python 补充-decode和encode
1. decode与encode转码 在Python3中默认编码就是uncode,encode转成Byte类型 在Python2中默认编码就是ascii window下默认编码是GBK decode( ...
- ACM题目————吝啬的国度
描述 在一个吝啬的国度里有N个城市,这N个城市间只有N-1条路把这个N个城市连接起来.现在,Tom在第S号城市,他有张该国地图,他想知道如果自己要去参观第T号城市,必须经过的前一个城市是几号城市(假设 ...
- rapidminer 数据导入及几个算子简单应用
rapidminer 数据导入及几个算子简单应用 一. 数据集选择 本次实验选择的数据集为: bank-data.csv 其中有600条数据 结构如下图: 二.数据集文件格式转换 Rapidminer ...
- 2016年12月16日 星期五 --出埃及记 Exodus 21:11
2016年12月16日 星期五 --出埃及记 Exodus 21:11 If he does not provide her with these three things, she is to go ...
- hao dongxi
asp.net页面间传值方式后台 asp.net页面间传值的几种方法 利用JQuery的$.ajax()可以很方便的调用asp.net的后台方法. json的使用 前后台统一以对象的方式编程 ASP. ...
- Infragistics UltraGrid的使用
OL SDK:http://help.infragistics.com/ 资料参考:http://blog.csdn.net/andy_212/article/details/4019895 http ...
- C++vector迭代器失效的问题
转载:http://blog.csdn.net/olanmomo/article/details/38420907 转载:http://blog.csdn.net/stpeace/article/de ...
- 4-JS对象
js中哪些是对象 除了字符串.数字.false.true.null和undefined之外,JavaScript中的值都是对象 原型(prototype) 每一个JavaScript对象(null除外 ...
- 复旦大学2014--2015学年第一学期高等代数I期末考试情况分析
一.期末考试成绩班级前几名 金羽佳(92).包振航(91).陈品翰(91).孙浩然(90).李卓凡(85).张钧瑞(84).郭昱君(84).董麒麟(84).张诚纯(84).叶瑜(84) 二.总成绩计算 ...
- HDU 5671 Matrix
Matrix Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Su ...