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 15152. ๋ฌธ์์ด ์ฌ๋ผ์ด์ฑ [::-1]์ผ๋ก ๋นํธ๋ฅผ ๋ค์ง์
16163. int(reversed_binary, 2)๋ก ๋ค์ง์ ์ด์ง์ ๋ฌธ์์ด์ ๋ค์ ์ ์๋ก ๋ณํํจ
1717"""
18-
1918class Solution :
2019 def reverseBits (self , n : int ) -> int :
2120
@@ -24,12 +23,29 @@ def reverseBits(self, n: int) -> int:
2423 reversed_binary = binary [::- 1 ]
2524
2625 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 )
2732"""
2833<Solution 2>
2934
30- Time Complexity:
35+ Time Complexity: O(1)
36+ - ๊ฐ ๋ฐ๋ณต์์ ๋นํธ ์ฐ์ฐ์ ์์ ์๊ฐ์ด ๊ฑธ๋ฆผ
3137
32- Space Complexity:
38+ Space Complexity: O(1)
39+ - ์ฌ์ฉ๋๋ ๋ณ์๋ result์ ์
๋ ฅ๊ฐ n๋ฐ์ ์์
3340
3441ํ์ด ๋ฐฉ๋ฒ:
42+ - ...
3543"""
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