We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 03ef4b9 commit cd70330Copy full SHA for cd70330
โreverse-bits/thispath98.pyโ
@@ -0,0 +1,26 @@
1
+class Solution:
2
+ def reverseBits(self, n: int) -> int:
3
+ """
4
+ Intuition:
5
+ ๋นํธ๋ฅผ ์ญ์์ผ๋ก ์ํํ๋ค.
6
+ answer์๋ ์ต๋๊ฐ(2^31)๋ถํฐ ์ต์๊ฐ(2^0)์ผ๋ก ๊ฐ์ํ๋
7
+ ๋ฐฉ์์ผ๋ก ์ ๋ฐ์ดํธํ๋ค.
8
+
9
+ Time Complexity:
10
+ O(N):
11
+ n์ 1๋ฒ ์ํํ๋ฉฐ ๋ต์ ์ฐพ์ผ๋ฏ๋ก,
12
+ O(N)์ ์๊ฐ๋ณต์ก๋๊ฐ ์์๋๋ค.
13
14
+ Space Complexity:
15
+ O(1):
16
+ answer์ ๊ฐ์ ์ ๋ฐ์ดํธ ํ๋ฏ๋ก, ์์์
17
+ ๊ณต๊ฐ๋ณต์ก๋๊ฐ ์์๋๋ค.
18
19
+ Key takeaway:
20
+ ์ซ์๋ฅผ binary string์ผ๋ก ๋ง๋๋ bin() ๋ฉ์๋๋ฅผ
21
+ ์๊ฒ ๋์๋ค.
22
23
+ answer = 0
24
+ for i, bit in enumerate(bin(n)[2:][::-1]):
25
+ answer += int(bit) * 2 ** (31 - i)
26
+ return answer
0 commit comments