leadcode of Hot100 series --136. The numbers appear only once

Because just wrote a 461 title before, and that this problem is very similar to
No. 461 with a different topic or to count two different parts
that need to exclude the subject as part of, and find a different section.
It can be reused at the XOR characteristics:
1, the same is 0, so, a ^ a = 0.
2, is different from 0,, 0 ^ b = b.
So, there are several hypothesis array is A, A, b,
---> A ^ A ^ b = b, then b is not the same as the number looking for.
(The final result of the exclusive or exclusive or nothing to do with the order, i.e., a ^ b ^ c = a ^ c ^ b = b ^ c ^ a).
Attach Code:

int singleNumber(int* nums, int numsSize){
    int i = 1;
    for (; i<numsSize; i++)
    {
        nums[0] ^= nums[i];
    }
    return nums[0];
}

Note: There is no definition of a new variable, using nums [0] as the return value, is performed in order to save time with a little (less the last calculated), less memory consumption point.
If you where to save another, please enlighten me when the execution! Thank you!

Guess you like

Origin www.cnblogs.com/payapa/p/11100422.html