We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 2bf8e97 commit c8ef9a6Copy full SHA for c8ef9a6
โnumber-of-1-bits/soobing2.tsโ
@@ -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