-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMain3.java
More file actions
51 lines (43 loc) · 1.01 KB
/
Main3.java
File metadata and controls
51 lines (43 loc) · 1.01 KB
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
package JZOfferTuJi;
public class Main3 {
public int[] countBits(int n) {
int[] bits = new int[n+1];
for(int i=0; i<=n; i++){
bits[i] = countOnes(i);
}
return bits;
}
private int countOnes(int x){
int count = (x&1)==1? 1 : 0;
while (x>0){
if((x>>>1&1)==1){
count++;
}
}
return count;
}
}
/*
Brian Kernighan 算法
原理:对于任意整数x,令x=x&(x-1),该运算将x的二进制表示的最后一个1变成0。
因此,对x重复该操作,直到x变为0,则操作次数即为x的[一比特数]。
*/
class Main3_1{
public int[] countBits(int n){
int[] bits = new int[n+1];
for(int i = 0; i<=n; i++){
bits[i] = countOnes(i);
}
return bits;
}
private int countOnes(int x){
int ones = 0;
while (x > 0){
x &= (x-1);
ones++;
}
return ones;
}
}
class Main3_2{
}