【LeetCode】137. 只出现一次的数字 II

class Solution {
public:
    int singleNumber(vector<int>& nums) {
        int ans = 0;
        int total = 0;
        for(int i = 0; i<32 ; ++i)
        {
            total = 0;
            for(int j=0; j<nums.size(); ++j)
            {
                total+=(nums[j]>>i)&1;   //按位统计某一位的数能否被3整除
            }
            if(total%3) //不能整除,则答案出现在这一位上
                ans|=(1<<i);
        }
        return ans;
    }
};

 

posted @ 2022-02-05 17:05  Jerry2km1  阅读(40)  评论(0)    收藏  举报