当前位置:网站首页>The sword refers to Offer 56 - II. Number of occurrences of a number in an array II (bit operation)

The sword refers to Offer 56 - II. Number of occurrences of a number in an array II (bit operation)

2022-08-09 13:54:00 to raise pigs

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

版权声明
本文为[to raise pigs]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/221/202208091244510438.html