Solution to Single Number II based on bit operation


  • 0
    T
    
    class Solution {
    public:
        int singleNumber(vector<int>& nums) {
            int acc1=0; 
            int acc2=0;
           
            for(int i=0;i<nums.size();++i){
                acc2 ^= acc1 & nums[i];
                acc1 ^= nums[i];
                int mask=~(acc2&acc1);
                acc1 &= mask;
                acc2 &= mask;
            }
            return acc1; 
        }
    };
    

Log in to reply
 

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