Single Number
Given an array of integers, every element appears twice except for one. Find that single one.
一个数组中只有一个整数是只出现了一次,其余均为两次,找出那个数。
直接用异或即可,因为如果两个数相同的话异或结果为0.
1 class Solution { 2 public: 3 int singleNumber(int A[], int n) { 4 int ret = A[0]; 5 for (int i = 1; i < n; ++i) { 6 ret ^= A[i]; 7 } 8 return ret; 9 } 10 };