Single Number II

Given an array of integers, every element appears three times except for one. Find that single one.

/**
     * 将整数所有位相加对3取余,剩下的就是只出现一次的数
     * @param A
     * @return
     */
    public int singleNumber(int[] A) {
        int sum[] = new int[32];                    //保存整数32位,A中所有数每一位相加的和
        int res = 0;                                //出现一次的数
        for(int i = 0; i < 32; i++){
            for(int j = 0; j < A.length; j++){
                sum[i] += ((A[j] >>> i) & 1);
            }//for
            sum[i] %= 3;
            res += (sum[i] << i);
        }//for
        
        return res;
    }
原文地址:https://www.cnblogs.com/luckygxf/p/4389022.html