File tree Expand file tree Collapse file tree 1 file changed +34
-0
lines changed
Expand file tree Collapse file tree 1 file changed +34
-0
lines changed Original file line number Diff line number Diff line change 1+ /**
2+ * @link https://leetcode.com/problems/number-of-1-bits/description/
3+ *
4+ * ์ ๊ทผ ๋ฐฉ๋ฒ :
5+ * - n์ 2๋ก ๋๋๋ฉด์ ๋๋จธ์ง๊ฐ 1์ธ ๊ฒฝ์ฐ ์นด์ดํธ๋ฅผ ์
๋ฐ์ดํธํ๋ค.
6+ *
7+ * ์๊ฐ๋ณต์ก๋ : O(log(n))
8+ * - ์ซ์์ ๋นํธ ๊ธธ์ด๋งํผ ๋ฐ๋ณต
9+ *
10+ * ๊ณต๊ฐ๋ณต์ก๋ : O(1)
11+ * - ๊ณ ์ ๋ ๋ณ์๋ง ์ฌ์ฉ
12+ */
13+ function hammingWeight ( n : number ) : number {
14+ let bitCount = 0 ;
15+ while ( n >= 1 ) {
16+ bitCount += n % 2 ;
17+
18+ n = Math . floor ( n / 2 ) ;
19+ }
20+
21+ return bitCount ;
22+ }
23+
24+ // ๋นํธ ์ฐ์ฐ์ ํ์ฉํ๋ ๋ฐฉ๋ฒ
25+ function hammingWeight ( n : number ) : number {
26+ let bitCount = 0 ;
27+ while ( n >= 1 ) {
28+ bitCount += n & 1 ;
29+
30+ n >>>= 1 ;
31+ }
32+
33+ return bitCount ;
34+ }
You canโt perform that action at this time.
0 commit comments