LeetCode 268: Missing Number

Missing number. Given an array containing n distinct numbers taken from 0, 1, 2, ... n, find the one that is missing from the array.

for example:

    Given nums = [0, 1, 3] return 2;

// This one is also bit manipulation.
int missingNumber(vector<int>& nums) {
    int n = nums.size();
    int result = 0;
    for(int i = 0; i < n; ++i) {
        result ^= i ^ nums[i];
    }
    return result ^ n;
}

你可能感兴趣的:(LeetCode 268: Missing Number)