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

来源:互联网 发布:乐乎公寓电话 编辑:程序博客网 时间:2024/05/29 13:09

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;    }};
0 0
原创粉丝点击