您的位置:首页 > 其它

leetcode---Single Number II---二进制

2016-10-04 20:00 405 查看
Given an array of integers, every element appears three times except for one. Find that single one.

Note:

Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?

class Solution {
public:
int singleNumber(vector<int>& nums)
{
int n = nums.size();
int b[32];
memset(b, 0, sizeof(b));
int ans = 0;
for(int i=0; i<32; i++)
{
for(int j=0; j<n; j++)
{
b[i] += (nums[j] >> i) & 1;
}
ans |= b[i] % 3 << i;
}
return ans;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: