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?

方法一:

首先计算nums数组中所有数字的异或,记为xor
令lowbit = xor & -xor,lowbit的含义为xor从低位向高位,第一个非0位所对应的数字
例如假设xor = 6(二进制:0110),则-xor为(二进制:1010,-6的补码)
则lowbit = 2(二进制:0010)

 class Solution {
public:
vector<int> singleNumber(vector<int>& nums) {
int size=nums.size();
if(size==||nums.empty())
return vector<int>();
int diff=;
for(auto &ele:nums)
diff^=ele;
vector<int> res(,);
diff&=-diff;//从低位向高位,第一个非0位所对应的数字
for(auto &ele:nums)
if(diff&ele)
res[]^=ele;
else
res[]^=ele;
return res;
}
};

方法二:

 class Solution {
public:
vector<int> singleNumber(vector<int>& nums) {
int size=nums.size();
if(size==||nums.empty())
return vector<int>();
int sum=;
for(auto &ele:nums)
sum^=ele;
vector<int> res(,);
//从低位向高位,第一个非0位所对应的数字
int n=;
while((sum&n)==)
n=n<<;
for(auto &ele:nums)
if(n&ele)
res[]^=ele;
else
res[]^=ele;
return res;
}
};

LeetCode 260 Single Number III 数组中除了两个数外,其他的数都出现了两次,找出这两个只出现一次的数的更多相关文章

  1. LeetCode 137 Single Number II 数组中除了一个数外,其他的数都出现了三次,找出这个只出现一次的数

    Given an array of integers, every element appears three times except for one, which appears exactly ...

  2. 260 Single Number III 数组中除了两个数外,其他的数都出现了两次,找出这两个只出现一次的数

    给定一个整数数组 nums,其中恰好有两个元素只出现一次,其他所有元素均出现两次. 找出只出现一次的那两个元素.示例:给定 nums = [1, 2, 1, 3, 2, 5], 返回 [3, 5].注 ...

  3. 137 Single Number II 数组中除了一个数外,其他的数都出现了三次,找出这个只出现一次的数

    给定一个整型数组,除了一个元素只出现一次外,其余每个元素都出现了三次.求出那个只出现一次的数.注意:你的算法应该具有线性的时间复杂度.你能否不使用额外的内存来实现?详见:https://leetcod ...

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

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

  5. [LeetCode] 260. Single Number III 单独数 III

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

  6. [LeetCode] 260. Single Number III(位操作)

    传送门 Description Given an array of numbers nums, in which exactly two elements appear only once and a ...

  7. Java [Leetcode 260]Single Number III

    题目描述: Given an array of numbers nums, in which exactly two elements appear only once and all the oth ...

  8. Leetcode 260 Single Number III 亦或

    在一个数组中找出两个不同的仅出现一次的数(其他数字出现两次) 同样用亦或来解决(参考编程之美的1.5) 先去取出总亦或值 然后分类,在最后一位出现1的数位上分类成 ans[0]和ans[1] a&am ...

  9. [LeetCode#260]Single Number III

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

随机推荐

  1. Azure上部署FTP服务

    FTP是个比较复杂的协议,其协议分为控制层和数据层,工作模式分为主动和被动两种模式. 在默认的Active模式下其工作原理如下: 可以看到,客户端发起FTP的请求道服务器端,FTP的端口是21.用户在 ...

  2. shell入门-uniq去重复和tee重定向

    命令:uniq 选项:-c 显示重复数量 说明:去重复,不sort多个功能,显示几个重复 命令:tee 说明:重定向加上双重输出 [root@wangshaojun ~]# cat 2.txt1222 ...

  3. tomcat jsp 数字串传值异常问题

    1.在下面的jsp内嵌java代码去除num之前,有某Controller已经有了操作:      request.getSession().setAttribute("num", ...

  4. 《Spring实战》系列之Bean的装配-Days02

    2.1 回顾 对于我第一天在bean的装配中写的,是一些基本的语法或者是Spring本身的一些规定,但是我没有对此进行深究.接下来就让我们仔细的讨论一下细节问题.和传统的类的定义和方法的调用做一些比较 ...

  5. 第八篇 elasticsearch链接mysql自动更新数据库

    增量更新 input { jdbc { jdbc_driver_library => "D:\tools\mysql\mysql-connector-java-5.1.45/mysql ...

  6. p4503&bzoj3555 企鹅QQ

    传送门(洛谷) 传送门(bzoj) 题目 PenguinQQ是中国最大.最具影响力的SNS(Social Networking Services)网站,以实名制为基础,为用户提供日志.群.即时通讯.相 ...

  7. Python + winpcap抓包和发包

    winpcapy Python的winpcapy库可以简单地实现收发Layer2层(数据链路层,以太网)数据. winpcapy主页:https://github.com/orweis/winpcap ...

  8. iis应用程序池没有fromwork 4.0-----安装iis

    找到已经安装的目录  C:\Windows\Microsoft.NET\Framework\v4.0.30319  以管理员身份运行一下就ok 安装iis 控制面板-程序与功能-打开与关闭window ...

  9. JsonParse类

    using System.Data; using Newtonsoft.Json; using Newtonsoft.Json.Converters; using System; using Syst ...

  10. git 修改远程仓库源

    自己已经写好了一个项目,想上传到 github github 创建新项目 新建 README.md , LICENSE 本地项目添加 github 远程仓库源 不是git项目 git remote a ...