Skip to content

Commit 07db313

Browse files
author
sap
committed
number-of-1-bits solution
1 parent dca8310 commit 07db313

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

number-of-1-bits/seona926.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* @param {number} n
3+
* @return {number}
4+
*/
5+
let hammingWeight = function (n) {
6+
let count = 0;
7+
let sum = n;
8+
9+
while (sum > 0) {
10+
// n에서 가장 오른쪽 비트가 1인 경우 count 증가
11+
if (sum % 2 === 1) {
12+
count++;
13+
}
14+
// sum을 2로 나누어서 다음 비트를 확인
15+
sum = Math.floor(sum / 2);
16+
}
17+
18+
return count;
19+
};

0 commit comments

Comments
 (0)