leetcode 137: Single Number II
2015-08-17 23:59
344 查看
Learned from others. Calculate the single number bit by bit. Add the same bit of all numbers and mod 3 to get the bit of that single number.
class Solution {
public:
int singleNumber(vector<int>& nums) {
int res=0;
for(int i=0;i<32;i++)
{
int sum=0;
for(int j=0;j<nums.size();j++)
sum+=(nums[j]>>i)&1;
res|=(sum%3)<<i;
}
return res;
}
};
class Solution {
public:
int singleNumber(vector<int>& nums) {
int res=0;
for(int i=0;i<32;i++)
{
int sum=0;
for(int j=0;j<nums.size();j++)
sum+=(nums[j]>>i)&1;
res|=(sum%3)<<i;
}
return res;
}
};
相关文章推荐
- gpio 相关中文文档
- 微信公众帐号开发之环境搭建php+nginx+mysql
- 开机自动启动程序的几种方法
- LVS+keepalived实现双机热备
- 闲话js前端框架(1)——从avalonjs的模板说起
- [leetcode] 227.Basic Calculator II
- HDU 1796 How many integers can you find(容斥原理+二进制/dfs)
- java基本输入输出编程:文本界面的输入与输出
- poj3281 dining 经典最大流建模方法
- NYOJ 64 鸡兔同笼
- UVALive 6838 Flipping Parentheses // 线段树 区间修改 最值查询
- C/C++获取本地时间常见方法
- 让大家学会复用
- Codeforces Round #249 (Div. 2) (模拟)
- C/C++获取本地时间常见方法
- Centos 6.4 KVM安装和配置
- Android 双进程Service常驻后台,无惧“一键清理
- apache和IIS同时存在,apache建立多个网站
- Codeforces Gym 100431B Binary Search 搜索+组合数学+高精度
- JQuery学习笔记 选择器 序