[leetcode] Single Number
Given an array of integers, every element appears twice except for one. Find that single one.
Note:
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
思路:
这题没什么难度,只要知道异或的操作就可以了
题解:
1 class Solution 2 { 3 public: 4 int singleNumber(int A[], int n) 5 { 6 int single = A[0]; 7 for(int i=1;i<n;i++) 8 single = single ^ A[i]; 9 return single; 10 } 11 };