LeetCode 137 - Single Number II - 题解/Solution

https://leetcode.com/problems/single-number-ii/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/**
* 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?
*
* @author dongyuxi
*
*/
public class Solution {
public int singleNumber(int[] A) {
if (null == A || 1 != A.length % 3) {
return 0;
}

int result = 0;
for (int i = 0; i < 32; i++) {
int bitCount = 0;
for (int j = 0; j < A.length; j++) {
if (((A[j] >> i) & 0x01) == 1) {
bitCount++;
}
}
result |= (bitCount % 3) << i;
}

return result;
}
}


支付宝 微信
文章目录