Skip to content

Commit 5765506

Browse files
authored
Create 191-number-of-1-bits.js
1 parent a6b6116 commit 5765506

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

191-number-of-1-bits.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* @param {number} n - a positive integer
3+
* @return {number}
4+
*/
5+
const hammingWeight = function(n) {
6+
let str = (n >>> 0).toString(2)
7+
let count = 0
8+
for(let i = 0; i < str.length; i++) {
9+
if(str.charAt(i) === '1') {
10+
count += 1
11+
}
12+
}
13+
return count
14+
};

0 commit comments

Comments
 (0)