260. Single Number III
来源:互联网 发布:淘宝打折那个软件 编辑:程序博客网 时间:2024/04/26 03:56
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?
思路:异或运算以及位运算的使用。
class Solution {public:vector<int> singleNumber(vector<int>& nums) {vector<int> res(2, 0);const int size = nums.size();if (size < 2)return nums;int tmp = 0;for (int i = 0; i < size; i++)tmp ^= nums[i];int p = 0;while ((tmp & 1) == 0){tmp >>= 1;p++;}tmp = 1 << p;for (int i = 0; i < size; i++){if ((nums[i] & tmp) == 0){res[0] ^= nums[i];}elseres[1] ^= nums[i];}return res;}};
0 0
- 260.Single Number III
- 260. Single Number III
- 260. Single Number III
- 260. Single Number III
- 260. Single Number III
- 260. Single Number III
- 260. Single Number III
- 260. Single Number III
- 260. Single Number III
- 260. Single Number III
- 260. Single Number III
- 260. Single Number III
- 260. Single Number III
- 260. Single Number III
- 260. Single Number III
- 260. Single Number III
- 260. Single Number III
- 260. Single Number III
- 分布式TensorFlow:在Spark上将谷歌的深度学习库进行尺度变换
- spring boot 使用velocity模板
- sunlggggJAVA学习
- Git 常用命令整理
- HTTP协议中 POST和GET的区别
- 260. Single Number III
- 如何利用Plxmon工具进行在线烧录PCI卡的EEPROM
- <c:foreach>与checkbox开发中遇到问题
- mongodb怎样给本地数据库添加用户名密码和修改账号密码?
- Android中仿淘宝首页顶部滚动自定义HorizontalScrollView定时水平自动切换图片
- JMX
- java二分查找算法
- mysql计划任务
- android自定义控件江河画卷,以及ListView百叶窗效果.