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
+ let hammingWeight = function ( n ) {
2
+ return DecToBinAndGetSetBits ( n ) ;
3
+ } ;
4
+
5
+ // TC : O(log n) | SC : O(1)
6
+
7
+ let DecToBinAndGetSetBits = ( n ) => {
8
+ let targetNum = n ;
9
+ let result = 0 ;
10
+ while ( targetNum > 0 ) {
11
+ let remainder = targetNum % 2 ;
12
+ if ( remainder === 1 ) result += 1 ;
13
+ targetNum = parseInt ( targetNum / 2 ) ;
14
+ }
15
+ return result ;
16
+ } ;
17
+
18
+ // TC : O(log n) | SC : O(log n)
19
+ // ๊ทผ๋ฐ ์ฌ์ค split ๋ฉ์๋ ์์ฒด๋ o(n)์ธ๋ฐ, toString๊ณผ์ ์ ํตํด log(n)์ ๊ฐ์๋งํผ ๋์๋ฒ๋ฆฐ ๊ฒ์ด๋ฉด o(log n)์ด๋ผ๊ณ ํ๊ธฐํด๋ ๋๋๊ฑธ๊น?
20
+
21
+ // let DecToBinAndGetSetBits = (n) => {
22
+ // let target = n;
23
+ // let bin = n.toString(2);
24
+ // return bin.split("").filter((el) => el == 1).length
25
+ // }
26
+
27
+ // TC : O(log n) | SC : O(log n)
28
+
29
+ // let DecToBinAndGetSetBits = (n) => {
30
+ // let target = n;
31
+ // let bin = n.toString(2);
32
+ // let matches = bin.match(/1/g);
33
+ // return matches.length
34
+ // }
You canโt perform that action at this time.
0 commit comments