Using XOR Sum to expedite the computing: x^y=sum, thus sum^x=y


  • 3
    A
    class Solution {
    public:
        int missingNumber(vector<int>& nums) {
            int result = 0;
            for(int i=0;i<nums.size();i++){
                result ^= nums[i];
            }
            for(int i=0;i<=nums.size();i++){
                result ^= i;
            }
            return result;
        }
    };

Log in to reply
 

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