Single Number III
2015-08-26 04:05
134 查看
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?
Solution:
class Solution { public: vector<int> singleNumber(vector<int>& nums) { assert(nums.size() >= 2); int a = 0; for (auto n : nums) a ^= n; int nbit = (a&(a-1))^a; int b = 0; for (auto n : nums) if (n & nbit) b ^= n; return vector<int>({b, a^b}); } };
相关文章推荐
- UIDatePicker 使用方法
- mysql手工添加windows服务
- php实现post和get
- MySQL-python “error: command 'gcc' failed with exit status 1”错误
- PHP中可以静态调用非静态方法么?(诡异的调用)
- 【小熊刷题】Convert Sorted List to Balanced Binary Search Tree<可再复习>
- SpringMVC的Ajax提交
- Spring之Aop与Ioc
- github开源php项目
- empty(), is_null(), isset()(区别)
- Majority Element II
- 愿少时无遗憾留低 愿余生有艺术伴随
- Linux自行学习命令1
- 安装启动 Supervisor
- [ACdream 1211 Reactor Cooling]无源无汇有上下界的可行流
- fontcreator 预览放大
- @RestController实现spring自己风格的rest的webservice(不用jax-rs包)
- [LeetCode#44]Wildcard Matching
- php大力力 [019节]php分页类的学习
- mockito测试入门