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 ...
随机推荐
- c# 客户端
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- 使用VisualSVN建立SVN Server
首先去官网下载安装包.http://subversion.apache.org/packages.html找到windows的,选择VisualSVN->VISUALSVN SERVER 双击开 ...
- Task schedule 分类: 比赛 HDU 查找 2015-08-08 16:00 2人阅读 评论(0) 收藏
Task schedule Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- 周赛-Killing Monsters 分类: 比赛 2015-08-02 09:45 3人阅读 评论(0) 收藏
Killing Monsters Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) ...
- hiho 第117周 二分图多重匹配,网络流解决
描述 学校的秋季运动会即将开始,为了决定参赛人员,各个班又开始忙碌起来. 小Hi和小Ho作为班上的班干部,统计分配比赛选手的重任也自然交到了他们手上. 已知小Hi和小Ho所在的班级一共有N名学生(包含 ...
- 2016年10月15日 星期六 --出埃及记 Exodus 18:26
2016年10月15日 星期六 --出埃及记 Exodus 18:26 They served as judges for the people at all times. The difficult ...
- iis 500 解决方法
1 打开运行,输入cmd进入到命令提示符窗口. 2 进入到C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727 目录. 3 输入aspnet_regiis.exe ...
- Hashtable与ConcurrentHashMap区别
Hashtable与ConcurrentHashMap区别 ConcurrentHashMap融合了hashtable和hashmap二者的优势. hashtable是做了同步的,是线性安全的,(2) ...
- Linux 性能监测:介绍
看了某某教程.读了某某手册,按照要求改改某某设置.系统设定.内核参数就认为做到系统优化的想法很傻很天真:)系统优化是一项复杂.繁琐.长期的 工作,优化前需要监测.采集.测试.评估,优化后也需要测试.采 ...
- 【Unity3D游戏开发】GameObject.Find()、Transform.Find查找隐藏对象 (十)
GameObjectFindTransformFind查找游戏对象 前置条件 相关API 1 GameObjectFind 2 TransformFind 3 其他查找 实际测试 即使隐藏root节点 ...