Skip to content

Commit ca6916c

Browse files
committed
2: number of 1 bits
1 parent 22e0e0c commit ca6916c

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

number-of-1-bits/whewchews.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
function hammingWeight(n: number): number {
2+
// SC: log(1)
3+
let val = 0;
4+
while (n > 0) {
5+
val += n % 2;
6+
7+
// TC: log₂(n)
8+
n = Math.floor(n / 2);
9+
}
10+
11+
return val;
12+
}
13+
14+
// TC: O(log N)
15+
// SC: O(1)

0 commit comments

Comments
 (0)