-
Notifications
You must be signed in to change notification settings - Fork 8
/
NumberOf1Bits.java
72 lines (65 loc) · 1.79 KB
/
NumberOf1Bits.java
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package bitmanipulation;
// Source : https://leetcode.com/problems/number-of-1-bits/
// Id : 191
// Author : Fanlu Hai | https://github.com/Fanlu91/FanluLeetcode
// Date : 2020-07-02
// Topic : Bit Manipulation
// Level : Easy
// Other :
// Tips :
// Links :
// Result : 100% 100%
public class NumberOf1Bits {
/**
* In Java, there is no unsigned integer type.
* You need to treat n as an unsigned value.
* In Java, the compiler represents the signed integers using 2's complement notation.
*/
public int hammingWeight(int n) {
int res = 0;
int mask = 1;
for (int i = 0; i < 32; i++) {
if ((n & mask) != 0) {
res++;
}
mask <<= 1;
}
return res;
}
// 右移 32 次
// 100.00% 79.18%
public int hammingWeight1(int n) {
// public int hammingWeight(int n) {
int res = 0;
for (int i = 31; i >= 0; i--) {
if (((n >> i) & 1) != 0)
res++;
}
return res;
}
// 通过 n & (n - 1) 可以翻转最低有效比特为 1 的比特为 0
// 100% 100%
public int hammingWeight2(int n) {
int res = 0;
while (n != 0) {
n = n & (n - 1);
res++;
}
return res;
}
/**
* public static int bitCount(int i) {
* // HD, Figure 5-2
* i = i - ((i >>> 1) & 0x55555555);
* i = (i & 0x33333333) + ((i >>> 2) & 0x33333333);
* i = (i + (i >>> 4)) & 0x0f0f0f0f;
* i = i + (i >>> 8);
* i = i + (i >>> 16);
* return i & 0x3f;
* }
*/
public int hammingWeight3(int n) {
// public int hammingWeight(int n) {
return Integer.bitCount(n);
}
}