LeetCode 260 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:
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 数组中除了两个数外,其他的数都出现了两次,找出这两个只出现一次的数的更多相关文章
- LeetCode 137 Single Number II 数组中除了一个数外,其他的数都出现了三次,找出这个只出现一次的数
Given an array of integers, every element appears three times except for one, which appears exactly ...
- 260 Single Number III 数组中除了两个数外,其他的数都出现了两次,找出这两个只出现一次的数
给定一个整数数组 nums,其中恰好有两个元素只出现一次,其他所有元素均出现两次. 找出只出现一次的那两个元素.示例:给定 nums = [1, 2, 1, 3, 2, 5], 返回 [3, 5].注 ...
- 137 Single Number II 数组中除了一个数外,其他的数都出现了三次,找出这个只出现一次的数
给定一个整型数组,除了一个元素只出现一次外,其余每个元素都出现了三次.求出那个只出现一次的数.注意:你的算法应该具有线性的时间复杂度.你能否不使用额外的内存来实现?详见:https://leetcod ...
- LeetCode 260. Single Number III(只出现一次的数字 III)
LeetCode 260. Single Number III(只出现一次的数字 III)
- [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 ...
- [LeetCode] 260. Single Number III(位操作)
传送门 Description Given an array of numbers nums, in which exactly two elements appear only once and a ...
- Java [Leetcode 260]Single Number III
题目描述: Given an array of numbers nums, in which exactly two elements appear only once and all the oth ...
- Leetcode 260 Single Number III 亦或
在一个数组中找出两个不同的仅出现一次的数(其他数字出现两次) 同样用亦或来解决(参考编程之美的1.5) 先去取出总亦或值 然后分类,在最后一位出现1的数位上分类成 ans[0]和ans[1] a&am ...
- [LeetCode#260]Single Number III
Problem: Given an array of numbers nums, in which exactly two elements appear only once and all the ...
随机推荐
- 洛谷【P1104】生日(选择排序版)
题目传送门:https://www.luogu.org/problemnew/show/P1104 题目很简单,不过我是来讲选择排序的. 选择排序\((Selection sort)\)是一种简单直观 ...
- 串口编程3:使用串口读取GPS信息
关于GPS的使用,参考. 本文主要参考的博客,在此表示感谢!!! 主函数 主函数gps_main.c,这里便涉及到了串口的打开,读操作,以及调用了串口设置函数: #include <stdio. ...
- netty中的Channel、ChannelPipeline
一.Channel与ChannelPipeline关系 每一个新创建的 Channel 都将会被分配一个新的 ChannelPipeline.这项关联是永久性 的:Channel 既不能附加另外一个 ...
- BAT小米奇虎美团迅雷携程等等各大企业校招,笔试面试题。
类似在线测试的方式展示题目. 历年在线笔试试卷: 百度 http://www.nowcoder.com/paper/search?query=%E7%99%BE%E5%BA%A6 腾讯http:// ...
- xdu2017校赛F
Problem F Dogs of Qwordance Senior Backend R&D Engineers 问题描述 那年夏天,锘爷和杰师傅漫步在知春公园的小道上.他们的妻子.孩子牵 着 ...
- sql语句去重 最后部分没看 看1 有用
一 数据库 1.常问数据库查询.修改(SQL查询包含筛选查询.聚合查询和链接查询和优化问题,手写SQL语句,例如四个球队比赛,用SQL显示所有比赛组合:举例2:选择重复项,然后去掉重复项:) 数据库里 ...
- 使用Meshlab软件将点云(Point Cloud)转换为模型(Mesh)
使用Meshlab软件将点云(Point Cloud)转换为模型(Mesh) 启动Meshlab软件: 导入.ply点云文件: 接着点击: 弹出一个右侧边栏: 接着,计算每个点的法线: 输入100,点 ...
- 32、Differential Gene Expression using RNA-Seq (Workflow)
转载: https://github.com/twbattaglia/RNAseq-workflow Introduction RNAseq is becoming the one of the mo ...
- Luogu 2668 [NOIP2015]斗地主
打牌技术不精,没有把$A$放在顺子里面搜,WA了好长时间. 盗用大佬的一张图: 当时自己搜的时候没有把四张牌拆成三带一等情况. 然后还有一点就是四张三张都出完之后直接数一数剩下的一张两张牌还要多少次出 ...
- matlab 矩阵运算技巧
1.a=a(:) 作用:将矩阵转化成列向量 a=[a11 a12 a13 a=[a11 a21 a12 a22 a13 a23]^T a21 a22 a23] ...