File tree Expand file tree Collapse file tree 1 file changed +38
-0
lines changed Expand file tree Collapse file tree 1 file changed +38
-0
lines changed Original file line number Diff line number Diff line change
1
+ function hammingWeight ( n : number ) : number {
2
+
3
+ // ํ์ด 1:
4
+ // binary๋ก ๋ณํ ํ 1 count
5
+ // ์๊ฐ ๋ณต์ก๋: O(log n)
6
+ // ๊ณต๊ฐ ๋ณต์ก๋: O(log n)
7
+ const getResult1 = ( ) => {
8
+ const binaryNum = n . toString ( 2 ) ;
9
+
10
+ let count = 0 ;
11
+
12
+ for ( let i = 0 ; i < binaryNum . length ; i ++ ) {
13
+ if ( binaryNum . charAt ( i ) === '1' ) {
14
+ count ++
15
+ }
16
+ }
17
+
18
+ return count ;
19
+ } ;
20
+
21
+ // ํ์ด 2:
22
+ // ๋นํธ ์ฐ์ฐ์ ํ์ฉํ ์ ์๋ค๊ณ ํจ (From GPT)
23
+ // ์๊ฐ ๋ณต์ก๋: O(log n)
24
+ // ๊ณต๊ฐ ๋ณต์ก๋: O(1)
25
+ // const getResult2 = () => {
26
+ // let count = 0;
27
+
28
+ // while (n !== 0) {
29
+ // count += n & 1;
30
+ // n >>>= 1;
31
+ // }
32
+
33
+ // return count;
34
+ // }
35
+
36
+ return getResult1 ( ) ;
37
+ // return getResult2();
38
+ } ;
You canโt perform that action at this time.
0 commit comments