lintcode82 -Single Number I -easy

给出2*n + 1 个的数字,除其中一个数字之外其他每个数字均出现两次,找到这个数字。一次遍历,O(1)空间
 
利用异或运算的两个特性——1.自己与自己异或结果为0,2.异或满足交换律。 把所有数字异或起来就是答案。 
 
public class Solution {
    /*
     * @param A: An integer array
     * @return: An integer
     */
    public int singleNumber(int[] A) {
        // write your code here
        
        if(A == null || A.length == 0){
            return 0;
        }
        
        int res = 0;
        for(int i = 0; i < A.length; i++){
            res ^= A[i];
        }
        
        return res;
    }
}
原文地址:https://www.cnblogs.com/jasminemzy/p/7509684.html