LintCode 82.落单的数

LintCode 82.落单的数

描述

给出2*n + 1 个的数字,除其中一个数字之外其他每个数字均出现两次,找到这个数字。
n <= 100

答案

public class Solution {
    /**
     * @param A: An integer array
     * @return: An integer
     */
    public int singleNumber(int[] A) {
        // write your code here
        int m = 0;
        for(int i : A) {]
            m^=i;
        }
        return m;
    }
}

总结

位运算符包括:按位与&,按位或|,按位异或^,取反~,左移<<,右移>>
x^x=0, 0^x=x。

原文地址:https://www.cnblogs.com/wuxie0ne/p/10672690.html