Skip to content

Commit 0ada062

Browse files
committed
Number Of 1 Bits
1 parent 2683ab2 commit 0ada062

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

number-of-1-bits/hyejjun.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* @param {number} n
3+
* @return {number}
4+
*/
5+
var hammingWeight = function (n) {
6+
7+
let val = n.toString(2);
8+
9+
let res = 0;
10+
[...val].forEach((val) => res += parseInt(val))
11+
12+
return res;
13+
14+
};
15+
16+
// O(LogN)
17+
18+
console.log(hammingWeight(11));
19+
console.log(hammingWeight(128));
20+
console.log(hammingWeight(2147483645));

0 commit comments

Comments
 (0)