LeeCode-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:
- 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?
题目很简单,但是通过这个题目让我见识了LeetCode众多大牛的存在。
代码中,第一次了使用accumulate函数,然后还要bit_xor<int>()之类的C++11的function object。
此处如果diff = INT_MIN, 那么 -diff 恰好会溢出到 INT_MIN. 之前并没有注意过这个问题。
class Solution {
public:
vector<int> singleNumber(vector<int>& nums) {
int diff = accumulate(begin(nums), end(nums), , bit_xor<int>());
diff &= -diff;
vector<int> result = {, };
for (auto num: nums) {
result[!(diff & num)] ^= num;
}
return result;
}
};
LeeCode-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 ...
随机推荐
- JSON Web Token (JWT)生成Token及解密实战。
昨天讲解了JWT的介绍.应用场景.优点及注意事项等,今天来个JWT具体的使用实践吧. 从JWT官网支持的类库来看,jjwt是Java支持的算法中最全的,推荐使用,网址如下. https://githu ...
- Harry and magic string HDU - 5157 记录不相交的回文串对数
题意: 记录不相交的回文串对数 题解: 正着反着都来一遍回文树 用sum1[i] 表示到 i 位置,出现的回文串个数的前缀和 sun2[i]表示反着的个数 ans+=sum1[i-1]*sum2[i] ...
- js 实现多选
效果: html: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> < ...
- HDFS HA
- MySQL数据库之DDL(数据定义语言)
1.MySQL数据库之DDL创建.删除.切换 (1)查看所有数据库 show databases: (2)切换数据库 use 数据库名: (3)创建数据库 create database 数据库名: ...
- ubuntu 权限不够,解决办法,无法获得锁 /var/lib/dpkg/lock - open (11: 资源暂时不可用)
终端执行 sudo passwd root输入root 新密码执行命令 nano /usr/share/lightdm/lightdm.conf.d/50-ubuntu.conf末行添加 gr ...
- 本地git安装完成之后,从远程git服务器上面下载代码。报错SSL certificate problem:self signed certificate in certificate chain。
解决方案:打开git的控制端黑窗口,输入: git config --global http.sslVerify false 点击Entry之后,就会去掉git的ssl验证. 然后就可以正常的下载代码 ...
- Cats Transport
Cats Transport 现在有n座山,第i座山的坐标为\(d_i\),初始p个饲养员在山1,有m只猫,每只猫有一个属性\(h_i,t_i\)表示猫i 在\(h_i\)以及它在\(t_i\)时间后 ...
- Altera FPGA– Bit Slip
通过在接收端加延时,在延时间隙插入'0'或'1',以使最终接收和期望数据一致. BitSlip操作要注意几点: 1,BitSlip操作在rx_bitslip的上升沿即开始: 2,BitSlip操作开始 ...
- thinkphp 模型实例化
在ThinkPHP中,可以无需进行任何模型定义.只有在需要封装单独的业务逻辑的时候,模型类才是必须被定义的,因此ThinkPHP在模型上有很多的灵活和方便性,让你无需因为表太多而烦恼. 根据不同的模型 ...