Single Number III - LeetCode
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?
思路:对所有数进行一次异或运算。运算结果为所求的两个数的异或结果。
因为两个数是不同的,因此结果必不为0。设该结果为res,其二进制形式必有几位是1。而根据异或运算规则,所求的两个数在这些位上一定有一个是1,一个是0。我们从这些位中随便取1位,然后将所有的数根据在这一位上是0还是1分成两组。则所求的两个数必然分别在这两个组内,此时分别再进行一次异或运算,就得到了两个数。
代码中 res随机取二进制为1的一位 用的是 res &= -res;
可以这样做的原因是,一个数的负为它的反码加1, 如数00100100,反码是11011011,将其加一后变为11011100,可以看到,原数最右的一个1现在仍然是1,其他位与运算完后是0。这样两数与运算后结果为00000100,即只有最右的一个1。
此外,for (:)的写法也是第一次见。
class Solution {
public:
vector<int> singleNumber(vector<int>& nums) {
int res = ;
for (int num : nums)
res ^= num;
res &= -res;
vector<int> ans = {, };
for (int num : nums)
{
if (res & num)
ans[] ^= num;
else ans[] ^= num;
}
return ans;
}
};
Single Number III - LeetCode的更多相关文章
- Single Number III leetcode java
问题描述: Given an array of numbers nums, in which exactly two elements appear only once and all the oth ...
- Single Number III——LeetCode
Given an array of numbers nums, in which exactly two elements appear only once and all the other ele ...
- 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)
- leetcode 136. Single Number 、 137. Single Number II 、 260. Single Number III(剑指offer40 数组中只出现一次的数字)
136. Single Number 除了一个数字,其他数字都出现了两遍. 用亦或解决,亦或的特点:1.相同的数结果为0,不同的数结果为1 2.与自己亦或为0,与0亦或为原来的数 class Solu ...
- 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 ...
随机推荐
- 4 Template层-CSRF
1.csrf 全称Cross Site Request Forgery,跨站请求伪造 某些恶意网站上包含链接.表单按钮或者JavaScript,它们会利用登录过的用户在浏览器中的认证信息试图在你的网站 ...
- POJ 3041 Asteroids (二分图最小点覆盖集)
Asteroids Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 24789 Accepted: 13439 Descr ...
- 自定义RadioGrop,支持添加包裹着的RadioButton
控件类: package com.chinaCEB.cebView; import android.annotation.TargetApi; import android.content.Conte ...
- ogre3D学习基础19 --- 材质的继承,纹理的滚动与旋转
以上一节为基础,废话不多说. 首先新增一个节点,用于比较显示 //新增一个节点 ent = mSceneMgr->createEntity("Quad"); ent-> ...
- [转]核函数K(kernel function)
1 核函数K(kernel function)定义 核函数K(kernel function)就是指K(x, y) = <f(x), f(y)>,其中x和y是n维的输入值,f(·) 是从n ...
- sqlserver 获取一个月有多少天
--思路:给定日期的下一个月的1号减去1天,然后取datepart(DAY,dt) declare @dt varchar(10)select @dt='2013-11-20'select datep ...
- [python][django学习篇][4]django完成数据库代码翻译:迁移数据库(migration)
上一篇我们已经完成数据库的设计,但是仅仅是python语言,并没有真正创建了数据库表.翻译成数据库语言,真正创建数据库表由django manage.py来实现,这一过程专业术语:迁移数据库 切换到m ...
- Ansible实战之Nginx代理Tomcat主机架构
author:JevonWei 版权声明:原创作品 实验架构:一台nginx主机为后端两台tomcat主机的代理,并使用Ansible主机配置 实验环境 Nginx 172.16.252.82 Tom ...
- node.js express 4.x 安装指南(Express不是内部或外部命令解决方案)
前几天express 推出了4.0,得知这个消息,自己尝试了一下,突然发现用以前的文档上的操作出现了各种问题.结果只能去看文档,现在在这个给大家分享下4.0版本的安装. 先说下如果需要用express ...
- [HNOI2004][bzoj1212] L语言 [Trie+dp]
题面 传送门 思路 无后效性 显然,不管某个前缀的理解方式是怎么样的,如果它能被理解,那么前面的决策对于后面的决策而言都是等价的 因此这题可以DP DP方程 令$dp[i]$表示前缀i是否能被理解 那 ...