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?
題目
給出2*n + 1 個的數(shù)字虹统,除其中一個數(shù)字之外其他每個數(shù)字均出現(xiàn)兩次倒彰,找到這個數(shù)字攻泼。
樣例
給出** [1,2,2,1,3,4,3]**髓霞,返回 4
分析
顯然用異或就行了析砸,因為其他的數(shù)均出現(xiàn)兩次
代碼
public class Solution {
/**
*@param A : an integer array
*return : a integer
*/
public class Solution {
public int singleNumber(int[] nums) {
int ans =0;
int len = nums.length;
for(int i=0;i!=len;i++)
ans ^= nums[i];
return ans;
}
}
}