Skip to content

Commit c2fba06

Browse files
committed
number-of-1-bits solution
1 parent 9675dc5 commit c2fba06

1 file changed

Lines changed: 19 additions & 0 deletions

File tree

number-of-1-bits/hoonjichoi1.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+
4+
if (n == 1) {
5+
return 1;
6+
}
7+
8+
int curr = n, result = 1;
9+
while (curr > 1) {
10+
if (curr % 2 == 1) {
11+
result++;
12+
}
13+
curr = curr / 2;
14+
}
15+
16+
return result;
17+
}
18+
}
19+

0 commit comments

Comments
 (0)