Share my C++ solution,easy to understand


  • 0
    V
    class Solution {
    public:
        int singleNumber(vector<int>& nums) {
            int ret = 0, i = 0, j = 0;
            int n = nums.size(), intlen = sizeof(int) * 8;
            
            vector<int> cnt(intlen, 0);
            
            for (i = 0; i < n; ++i)
            {
                for (j = 0; j <intlen; ++j)
                {
                    cnt[j] += ((nums[i] >> j) & 1);
                    cnt[j] %= 3;
                    
                } 
            }
            
            for (i = 0; i < intlen; ++i)
                ret += (cnt[i] << i);
            
            return ret;
        }
    };

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.