Skip to content

Commit 9e6d539

Browse files
committed
feat: hammingWeight 문제풀이 추가
1 parent ee1ad8e commit 9e6d539

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

number-of-1-bits/hwanmini.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// 시간복잡도: O(log n)
2+
// 공간복잡도: O(log n)
3+
4+
const replaceZeroToEmptyString = (str) => str.replaceAll('0','')
5+
6+
7+
/**
8+
* @param {number} n
9+
* @return {number}
10+
*/
11+
var hammingWeight = function(n) {
12+
const binaryNum = n.toString(2)
13+
const replacedNumber = replaceZeroToEmptyString(binaryNum)
14+
return replacedNumber.length
15+
};
16+
17+
18+
console.log(hammingWeight(11));
19+
console.log(hammingWeight(128));
20+
console.log(hammingWeight(2147483645));

0 commit comments

Comments
 (0)