Skip to content

Commit cafa20c

Browse files
committed
Number of 1Bits
1 parent 2706dea commit cafa20c

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

number-of-1-bits/sunjae95.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* @description
3+
* time complexity: O(logN)
4+
* space complexity: O(1)
5+
* approach/strategy:
6+
* 1. decimal to binary
7+
*/
8+
9+
/**
10+
* @param {number} n
11+
* @return {number}
12+
*/
13+
var hammingWeight = function (n) {
14+
let answer = 0;
15+
16+
while (n > 0) {
17+
answer += n % 2;
18+
n = Math.floor(n / 2);
19+
}
20+
21+
return answer;
22+
};

0 commit comments

Comments
 (0)