剑指offer 56 出现一次的数 Java

数组中只有一个数只出现1次,其他的出现3次,找出这个数
public class OnceInOtherThree56 {
    public static void main(String[] args) {
        int[] arr = {1, 2, 3, 4, 1, 1, 2, 4, 4, 2};
        findThat(arr);
    }

    private static void findThat(int[] arr) {
        int[] bit = new int[32];
        for (int i = 0; i < arr.length; i++) {
            for (int j = 31; j >= 0; j--) {
                if ((arr[i] & 1) == 1) {
                    bit[j] += 1;
                }
                arr[i] = arr[i] >> 1;
            }
        }
        int temp = 0;
        for (int i = 0; i < 32; i++) {
            temp = temp << 1;
            temp += bit[i]%3;
        }
        System.out.println(temp);
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_43065507/article/details/99333957
今日推荐