File tree Expand file tree Collapse file tree 1 file changed +39
-0
lines changed
Expand file tree Collapse file tree 1 file changed +39
-0
lines changed Original file line number Diff line number Diff line change 1+ /**
2+ * @param n - ์์ ์ ์
3+ * @returns - ์ด์ง ๋ณํ ์ 1์ ๊ฐฏ์
4+ * @description
5+ * - ํ์ด 1: ์ง์ ์ด์ง์๋ก ๋ณํํ๋ ์์
์ ์ถ๊ฐ
6+ * - ์ด์ง ์ ๋ณํ ํ ๋ฐฐ์ด ๋ณ๊ฒฝ, 1๋ง ๋ฝ์๋ด์ ๊ธธ์ด ๋ฐํํ๊ธฐ
7+ * - ์๊ฐ๋ณต์ก๋: O(n), ๊ณต๊ฐ๋ณต์ก๋: O(n)
8+ */
9+
10+ // function hammingWeight(n: number): number {
11+ // if (n === 0) return 0;
12+ // let result = "";
13+ // while (n > 0) {
14+ // result = (n % 2) + result;
15+ // n = Math.floor(n / 2);
16+ // }
17+ // let count = 0;
18+ // for (const ch of result) {
19+ // if (ch === "1") {
20+ // count++;
21+ // }
22+ // }
23+ // return count;
24+ // }
25+
26+ function hammingWeight ( n : number ) : number {
27+ const bit = n
28+ . toString ( 2 )
29+ . split ( "" )
30+ . filter ( ( v ) => v === "1" ) ;
31+
32+ return bit . length ;
33+ }
34+
35+ const n = 2147483645 ;
36+
37+ hammingWeight ( n ) ;
38+
39+
You canโt perform that action at this time.
0 commit comments