As we know, each two same integer after xor operation will lead to 0x00000000. And if x is an integer, 0x00000000 ^ x = x. So we can take the adantage of xor property to solve this problem.
> File Name: SingleNumber.java > Author: Yao Zhang > Mail: [email protected] > Created Time: Sun 15 Nov 23:21:17 2015 ************************************************************************/ publicclass{ publicintsingNumber(int[] A){ if (A == null || A.length == 0){ return0; } int result = A[0]; for (int i = 1; i < A.length; i++){ result = result ^ A[i]; } return result; } }
近期评论