Bit manipulation


  • 0
    X

    Bit Manipulation
    solution #1:
    use bit operation.
    we know if we XOR two same numbers, the result will be zero.
    thus for this question we only need to XOR indices from 0 to n-1, and then we XOR the result with all the numbers in these array. since same number XOR together is zero, the remain number is the missing one.

    '''
    public int missingNumber(int[] nums) {
    int result = nums.length;
    for(int i = 0; i < nums.length; i++){
    result = result ^ nums[i];
    result = result ^ i;
    }
    return result;
    }

    '''


Log in to reply
 

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