Skip to content

Commit f9f21fd

Browse files
committed
number-of-1-bits solution
1 parent e6f65c0 commit f9f21fd

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

number-of-1-bits/chjung99.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* time: O(log n)
3+
* space: O(1)
4+
*/
5+
class Solution {
6+
public int hammingWeight(int n) {
7+
int cnt = 0;
8+
while (n != 0){
9+
if (n % 2 == 1) cnt ++;
10+
n = (int) (n/2);
11+
}
12+
return cnt;
13+
}
14+
}
15+

0 commit comments

Comments
 (0)