给出2*n + 1 个的数字,除其中一个数字之外其他每个数字均出现两次,找到这个数字。
样例
给出 [1,2,2,1,3,4,3],返回 4
挑战
一次遍历,常数级的额外空间复杂度
public class Solution {
/**
* @param A: An integer array
* @return: An integer
*/
public int singleNumber(int[] A) {
// write your code here
int res[] = new int[A.length];
for(int i=0;i<A.length;i++){
for(int j=0;j<A.length;j++){
if(A[i]==A[j]){
res[i]++;
}
}
}
for(int i=0;i<A.length;i++){
if(res[i]==1){
return A[i];
}
}
return 0;
}
}