Skip to content

Commit 2e3c615

Browse files
committed
solution Number of 1 Bits (#232)
#232
1 parent e4a226c commit 2e3c615

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
*
3+
* @param n
4+
*
5+
* ํ’€์ด 1
6+
*
7+
* function hammingWeight(n: number): number {
8+
* let parse = (n).toString(2);
9+
* let count = 0;
10+
* for (const item of parse){
11+
* if(+item ===1) count ++
12+
* }
13+
* return count
14+
* };
15+
*
16+
* ์ˆซ์ž๋ฅผ ๋ฌธ์ž์—ด๋กœ ๋ณ€ํ™˜ํ• ๋•Œ ์˜ค๋ฒ„ํ—ค๋“œ ๋ฐœ์ƒ
17+
* ๋น„ํŠธ ์—ฐ์‚ฐ์„ ์‚ฌ์šฉํ•ด์•ผํ• ๋“ฏ!
18+
*
19+
* ๊ฒ€์ƒ‰ํ•ด๋ณด๋‹ˆ Brian Kernighan ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์‚ฌ์šฉํ•˜๋ฉด ๋” ํšจ์œจ์ ์ด๋ผ๊ณ  ํ•œ๋‹ค
20+
*/
21+
22+
function hammingWeight(n: number): number {
23+
let count = 0;
24+
25+
while (n !== 0) {
26+
n &= (n - 1);
27+
count++;
28+
}
29+
30+
return count;
31+
}

0 commit comments

Comments
ย (0)