【Leetcode】Single Number II (Bit Manipulation)

来源:互联网 发布:淘宝哪家男装店好 编辑:程序博客网 时间:2024/06/05 20:32

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

Note:

Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?

具体分析参见位运算之找出奇特的数

直接上代码

public int singleNumber(int[] A) {int[] digit = new int[32];int result = 0;for (int i = 0; i < 32; i++) {for (int j = 0; j < A.length; j++) {digit[i] += (A[j] >> i) & 1;}}for (int i = 0; i < 32; i++)result |= (digit[i] % 3) << i;return result;}


0 0