题目:
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?
思路:
使用异或
Code:
public class SingleNum {
public static void main(String[] args) {
SingleNum s=new SingleNum();
int A[]={1,1,2};
System.out.println(s.singleNumber(A));
}
public int singleNumber(int[] A) {
int sn=0;
for(int i=0;i<A.length;i++){
sn=sn^A[i];
}
return sn;
}
}
异或符号: ^