Skip to content

Commit b84f940

Browse files
committed
add solution for number-of-1-bits
1 parent 19d9657 commit b84f940

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

number-of-1-bits/Ujoonnee.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
public int hammingWeight(int n) {
3+
int answer = 0;
4+
while (n > 0) {
5+
answer += n & 1;
6+
n >>= 1;
7+
}
8+
9+
return answer;
10+
}
11+
}
12+
13+
/*
14+
class Solution {
15+
public int hammingWeight(int n) {
16+
return Integer.toBinaryString(n).replace("0", "").length();
17+
}
18+
}
19+
*/

0 commit comments

Comments
 (0)