File tree Expand file tree Collapse file tree 1 file changed +19
-3
lines changed Expand file tree Collapse file tree 1 file changed +19
-3
lines changed Original file line number Diff line number Diff line change 15
15
2. ๋ฌธ์์ด ์ฌ๋ผ์ด์ฑ [::-1]์ผ๋ก ๋นํธ๋ฅผ ๋ค์ง์
16
16
3. int(reversed_binary, 2)๋ก ๋ค์ง์ ์ด์ง์ ๋ฌธ์์ด์ ๋ค์ ์ ์๋ก ๋ณํํจ
17
17
"""
18
-
19
18
class Solution :
20
19
def reverseBits (self , n : int ) -> int :
21
20
@@ -24,12 +23,29 @@ def reverseBits(self, n: int) -> int:
24
23
reversed_binary = binary [::- 1 ]
25
24
26
25
return int (reversed_binary , 2 )
26
+
27
+ # ์ฝ๋๋ฅผ ๊ฐ๊ฒฐํ๊ฒ ์ ๋ฆฌํ ๋ฒ์
28
+ class Solution :
29
+ def reverseBits (self , n : int ) -> int :
30
+
31
+ return int (format (n , '032b' )[::- 1 ], 2 )
27
32
"""
28
33
<Solution 2>
29
34
30
- Time Complexity:
35
+ Time Complexity: O(1)
36
+ - ๊ฐ ๋ฐ๋ณต์์ ๋นํธ ์ฐ์ฐ์ ์์ ์๊ฐ์ด ๊ฑธ๋ฆผ
31
37
32
- Space Complexity:
38
+ Space Complexity: O(1)
39
+ - ์ฌ์ฉ๋๋ ๋ณ์๋ result์ ์
๋ ฅ๊ฐ n๋ฐ์ ์์
33
40
34
41
ํ์ด ๋ฐฉ๋ฒ:
42
+ - ...
35
43
"""
44
+ class Solution :
45
+ def reverseBits (self , n : int ) -> int :
46
+ result = 0
47
+ for i in range (32 ):
48
+ result <<= 1 # ๊ฒฐ๊ณผ๋ฅผ ์ผ์ชฝ์ผ๋ก ํ ์นธ ๋ฐ๊ณ
49
+ result |= n & 1 # n์ ๋ง์ง๋ง ๋นํธ๋ฅผ ๊ฒฐ๊ณผ์ ์ถ๊ฐ
50
+ n >>= 1 # n์ ์ค๋ฅธ์ชฝ์ผ๋ก ํ ์นธ ๋ฐ์ด ๋ค์ ๋นํธ๋ก ์ด๋
51
+ return result
You canโt perform that action at this time.
0 commit comments