Skip to content

Commit c8ef9a6

Browse files
committed
feat(soobing): week3 > number-of-1-bits
1 parent 2bf8e97 commit c8ef9a6

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* ๋ฌธ์ œ ์œ ํ˜•
3+
* - Binary (๊ฐœ๋…์„ ์•Œ๊ณ ์žˆ๋Š”์ง€), ๊ธฐ๋ณธ์ ์ธ ๊ตฌํ˜„ ๋ฌธ์ œ
4+
*
5+
* ๋ฌธ์ œ ์„ค๋ช…
6+
* - ์ฃผ์–ด์ง„ ์ •์ˆ˜๋ฅผ 2์ง„์ˆ˜๋กœ ๋ณ€ํ™˜ํ–ˆ์„๋•Œ 1์˜ ๊ฐฏ์ˆ˜๋ฅผ ๊ตฌํ•˜๋Š” ๋ฌธ์ œ
7+
*
8+
* ์•„์ด๋””์–ด
9+
* 1) ๋‚˜๋ˆ„๊ธฐ 2๋ฅผ ํ†ตํ•ด 2์ง„์ˆ˜๋กœ ๋ณ€ํ™˜ํ•˜๊ณ  1์ธ ๊ฒฝ์šฐ ๊ฐฏ์ˆ˜๋ฅผ ์นด์šดํŠธํ•œ๋‹ค.
10+
*/
11+
function hammingWeight(n: number): number {
12+
let quotient = n;
13+
let count = 0;
14+
15+
while (quotient) {
16+
if (quotient % 2 === 1) count++;
17+
quotient = Math.floor(quotient / 2);
18+
}
19+
return count;
20+
}

0 commit comments

Comments
ย (0)