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:

  1. The order of the result is not important. So in the above example, [5, 3] is also correct.
  2. 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的更多相关文章

  1. leetcode 136. Single Number 、 137. Single Number II 、 260. Single Number III(剑指offer40 数组中只出现一次的数字)

    136. Single Number 除了一个数字,其他数字都出现了两遍. 用亦或解决,亦或的特点:1.相同的数结果为0,不同的数结果为1 2.与自己亦或为0,与0亦或为原来的数 class Solu ...

  2. leetcode 136 Single Number, 260 Single Number III

    leetcode 136. Single Number Given an array of integers, every element appears twice except for one. ...

  3. LeetCode 260. Single Number III(只出现一次的数字 III)

    LeetCode 260. Single Number III(只出现一次的数字 III)

  4. Single Number III(LintCode)

    Single Number III Given 2*n + 2 numbers, every numbers occurs twice except two, find them. Example G ...

  5. 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 ...

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

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

  7. [LeetCode] Single Number III 单独的数字之三

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

  8. LeetCode Single Number III

    原题链接在这里:https://leetcode.com/problems/single-number-iii/ 题目: Given an array of numbers nums, in whic ...

  9. [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 ...

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

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

随机推荐

  1. python:正则表达式 re

    #re正则的用法:match匹配从开头 search 取一个就回来了,findout取所以匹配的,slit分割 sub替换 #-*- coding:utf8 -*- # Auth:fulimei #r ...

  2. String类方法

    1.charAt(int index)  返回指定索引处的 char 值. 2. length() 返回此字符串的长度. 3.String replace(char oldChar, char new ...

  3. C++中关于cin、cin.get()、cin.getline()、getline()、gets()等函数的用法

    1.cin>> 用法1:最基本,也是最常用的用法,输入一个数字: 注意:>> 是会过滤掉不可见的字符(如 空格 回车,TAB 等) cin>>noskipws> ...

  4. 计算机学院大学生程序设计竞赛(2015’12)Polygon

    Polygon Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Su ...

  5. poj3372 Candy Distribution

    可以证明: f(k) = k *(k - 1)/ 2 (1 ≤ k ≤ n)是n的完全剩余系当且仅当n = 2 ^ t. http://poj.org/problem?id=3372

  6. 华东交通大学2016年ACM“双基”程序设计竞赛 1007

    Problem Description ACM小学妹在今天的暑假训练结束后,想看球赛放松一下.当他打开电脑时查询到联盟今天直播N场球赛,每场球赛的起止时间(S1,E1),(S2,E2),...,(SN ...

  7. EasyUI DataGrid 添加排序

    这个事例演示了如何在点击列头的时候排序DataGrid中全部的列可以通过点击列头被排序.你可以定义可以被排序的列.默认的,列不能被排序除非你设置sortable属性为TRUE,下面是例子:标记 < ...

  8. 2016年12月4日 星期日 --出埃及记 Exodus 20:25

    2016年12月4日 星期日 --出埃及记 Exodus 20:25 If you make an altar of stones for me, do not build it with dress ...

  9. JS脚本语言里的循环

    js脚本语言:  循环:(循环操作某一个功能(执行某段代码)) 四要素: 循环初始值  循环条件  状态改变  循环体 for(穷举  迭代) while 举例:(穷举) 与7相关的数 <scr ...

  10. [C语言](*p)++与 ++(*p)与 *p++ 与 ++*p

    首先亮明个人观点,不要认为这样写都算写的不好,还强调大神一般不写这种代码,可读性不高 其实是你的C语言基础太差,读不懂,大神还真的就是这么写的.可以看看C语言库函数中的一些写法 *P++ :*和++都 ...