forked from DaleStudy/leetcode-study
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEgonD3V.py
34 lines (26 loc) ยท 1.12 KB
/
EgonD3V.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
from unittest import TestCase, main
class Solution:
def reverseBits(self, n: int) -> int:
return self.solve(n)
"""
Runtime: 32 ms (Beats 80.50%)
Time Complexity: O(1)
- n์ str๋ก ๋ณํํ๋๋ฐ, n์ 32 bit ์ ์์ด๋ฏ๋ก O(32), upper bound
- zfill๋ก ๋ฌธ์์ด์ ๊ธธ์ด๋ฅผ 32๋ก ๋ง์ถ๋๋ฐ, O(32), upper bound
- ๋ฌธ์์ด์ ๋ค์ง๋๋ฐ ๋ง์ฐฌ๊ฐ์ง๋ก, O(32), upper bound
- ๋ค์ง์ ๋ฌธ์์ด์ ์ ์๋ก ๋ณํํ๋๋ฐ ๋ฌธ์์ด์ ๋น๋กํ๋ฉฐ ์ด ๊ธธ์ด๋ ์ต๋ 32์ด๋ฏ๋ก, O(32), upper bound
> O(32) * O(32) * O(32) * O(32) ~= O(1)
Memory: 16.50 (Beats 64.72%)
Space Complexity: O(1)
- ๊ฐ ๋จ๊ณ๋ง๋ค ์ต๋ ๊ธธ์ด๊ฐ 32์ธ ๋ฌธ์์ด์ด ์์๋ก ์ ์ฅ๋๋ฏ๋ก O(32) * 4
> O(32) * 4 ~= O(1)
"""
def solve(self, n: int) -> int:
return int(str(n).zfill(32)[::-1], 2)
class _LeetCodeTestCases(TestCase):
def test_1(self):
n = int("00000010100101000001111010011100")
output = 964176192
self.assertEqual(Solution.reverseBits(Solution(), n), output)
if __name__ == '__main__':
main()