c++ solution

  • 0
    class Solution {
        vector<int> singleNumber(vector<int>& nums) {
            vector<int> result(2,0);
            if(nums.size()<2) return result;
            int sum=0;
            for(int n:nums) sum^=n;
            int mask=1;
            while((sum&mask)==0) mask<<=1;
            for(int n:nums) {
                if((n&mask)==0) result[0]^=n;
                else result[1]^=n;
            return result;

Log in to reply

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