Single Number I
Given an array of integers, every element appears twice except for one.
Find that single one.
Your algorithm should have a linear runtime complexity.
Could you implement it without using extra memory?
Solution: XOR.
1 class Solution {
2 public:
3 int singleNumber(int A[], int n) {
4 int single = 0;
5 for(int i = 0; i < n; i++) {
6 single ^= A[i];
7 }
8 return single;
9 }
10 };

浙公网安备 33010602011771号