Leetcode 137. 只出现一次的数字 II

三进制半加器

class Solution {
public:
    int singleNumber(vector<int>& nums) {
        int A[32]={};
        int ans = 0;
        for (auto &x : nums)
            for (int i = 0; i < 32; ++i)
                if (x&(1 << i)) ++A[i];
        for (int i = 0; i < 32; ++i)
            if (A[i]%3) ans |= (1 << i);
        return ans;
    }
};

猜你喜欢

转载自blog.csdn.net/bendaai/article/details/80993006