Skip to content

Commit fe8e8bf

Browse files
committed
solve: number-of-1-bits 풀이
1 parent 1a194e7 commit fe8e8bf

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

number-of-1-bits/reach0908.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* 시간복잡도: O(n)
3+
* 공간복잡도: O(1)
4+
* @param {number} n
5+
* @return {number}
6+
*/
7+
var hammingWeight = function (n) {
8+
let num = n;
9+
10+
let count = 0;
11+
12+
while (num > 0) {
13+
const left = num % 2;
14+
15+
if (left === 1) {
16+
count += 1;
17+
}
18+
19+
num = Math.floor(num / 2);
20+
}
21+
22+
return count;
23+
};

0 commit comments

Comments
 (0)