算法题 落单的数

描述

给出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
        if(A==null || A.length==0){
            return -1;
        }
        int rst =0;
        for(int i=0;i<A.length;i++){
            //^ 异或操作  位数相同为0
            rst^=A[i];
        }
        
        return rst;
        
    }
}

 

 

posted @ 2018-05-09 16:36  月关莫利亚  阅读(282)  评论(0编辑  收藏  举报