Skip to content

Commit df1514a

Browse files
committed
Number of 1 bits Solution
1 parent 0adea9c commit df1514a

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

number-of-1-bits/naringst.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* @param {number} n
3+
* @return {number}
4+
*/
5+
6+
/**
7+
* Runtime: 59ms, Memory: 50.76MB
8+
*
9+
* Time complexity: O(logN)
10+
* -> While
11+
* Space complexity: O(logN)
12+
* -> To save binary.split()
13+
*
14+
* **/
15+
16+
var hammingWeight = function (n) {
17+
let binary = "";
18+
19+
while (n > 1) {
20+
let left = n % 2;
21+
binary += left.toString();
22+
n = Math.floor(n / 2);
23+
}
24+
binary = binary + n.toString();
25+
26+
const count = binary.split("1").length - 1;
27+
28+
return count;
29+
};

0 commit comments

Comments
 (0)