例子比如{1,1,2,2,3}找出3。
本题目考察的异或运算。
1、相同的数异或为0。 1^1=0
2、其他数与0异或为本身。0^1=0
1^1^2^2^3=3
public static int singleNumber(int[] A) { int result = A[0]; for(int i=1; i<A.length; i++){ result = result ^ A[i]; } return result; }
例子比如{1,1,2,2,3}找出3。
本题目考察的异或运算。
1、相同的数异或为0。 1^1=0
2、其他数与0异或为本身。0^1=0
1^1^2^2^3=3
public static int singleNumber(int[] A) { int result = A[0]; for(int i=1; i<A.length; i++){ result = result ^ A[i]; } return result; }