Skip to content

Commit 92d8382

Browse files
committed
feat: 191. Number of 1 Bits
1 parent f4393df commit 92d8382

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

number-of-1-bits/gwbaik9717.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Time complexity: O(logn)
2+
// Space complexity: O(1)
3+
4+
function hammingWeight(n) {
5+
let answer = 1;
6+
let current = n;
7+
8+
while (current > 1) {
9+
if (current % 2 !== 0) {
10+
answer++;
11+
}
12+
13+
current = Math.floor(current / 2);
14+
}
15+
16+
return answer;
17+
}

0 commit comments

Comments
 (0)