-
-
Notifications
You must be signed in to change notification settings - Fork 299
/
191.java
28 lines (27 loc) · 885 Bytes
/
191.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
__________________________________________________________________________________________________
sample 0 ms submission
public class Solution {
// you need to treat n as an unsigned value
public int hammingWeight(int n) {
int count =0;
while(n!=0){
count+=(n & 0x1);
n = n>>>1;
}
return count;
}
}
__________________________________________________________________________________________________
sample 31760 kb submission
public class Solution {
// you need to treat n as an unsigned value
public int hammingWeight(int n) {
int count = 0;
while(n!=0){
if((n&1) == 1){count++;}
n = n>>>1; //这里的无符号位移
}
return count;
}
}
__________________________________________________________________________________________________