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 d1500c2 commit 4b659e7Copy full SHA for 4b659e7
โnumber-of-1-bits/clara-shin.jsโ
@@ -0,0 +1,24 @@
1
+/**
2
+ * ์ซ์๋ฅผ ์ด์ง์๋ก ๋ณํํ๊ณ 1์ ๊ฐ์๋ฅผ ์ธ๋ ๋ฐฉ๋ฒ
3
+ * Follow up: ์ด ํจ์๊ฐ ์ฌ๋ฌ ๋ฒ ํธ์ถ๋๋ค๋ฉด?
4
+ *
5
+ * ๋จ์ํ ๋ชจ๋ ๋นํธ๋ฅผ ํ์ธํ๋ ๋ฐฉ๋ฒ: O(log n) ๋๋ 32๋นํธ ์ ์์ ๊ฒฝ์ฐ O(32)์ ์๊ฐ ๋ณต์ก๋
6
+ * โก๏ธ Brian Kernighan์ ์๊ณ ๋ฆฌ์ฆ: O(k)
7
+ * 1 ๋นํธ์ ์์ ๋น๋กํ์ฌ ์คํ ์๊ฐ์ด ๊ฒฐ์
8
+ */
9
+
10
11
+ * @param {number} n
12
+ * @return {number}
13
14
+var hammingWeight = function (n) {
15
+ let count = 0;
16
17
+ while (n !== 0) {
18
+ // n & (n-1)์ n์ ๋ง์ง๋ง 1 ๋นํธ๋ฅผ ์ ๊ฑฐ
19
+ n = n & (n - 1);
20
+ count++;
21
+ }
22
23
+ return count;
24
+};
0 commit comments