Single Number
Given 2 * n + 1 numbers, every numbers occurs twice except one, find it.
Example
Given [1,2,2,1,3,4,3], return 4
Challenge
One-pass, constant extra space.
Solution
- Time O(N), Space O(1)
x ^ x=0 和 x ^ 0=x
可将给定数组的所有数依次异或,最后保留的即为结果。
1 2 3 4 5 6 7 8 9 10 11 12 |
|