Skip to content

Commit d0ebb69

Browse files
committed
feat: solve #234 with python
1 parent fe5d3ca commit d0ebb69

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

โ€Žreverse-bits/EGON.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
from unittest import TestCase, main
2+
3+
4+
class Solution:
5+
def reverseBits(self, n: int) -> int:
6+
return self.solve(n)
7+
8+
"""
9+
Runtime: 32 ms (Beats 80.50%)
10+
Time Complexity: O(1)
11+
- n์„ str๋กœ ๋ณ€ํ™˜ํ•˜๋Š”๋ฐ, n์€ 32 bit ์ •์ˆ˜์ด๋ฏ€๋กœ O(32), upper bound
12+
- zfill๋กœ ๋ฌธ์ž์—ด์˜ ๊ธธ์ด๋ฅผ 32๋กœ ๋งž์ถ”๋Š”๋ฐ, O(32), upper bound
13+
- ๋ฌธ์ž์—ด์„ ๋’ค์ง‘๋Š”๋ฐ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ, O(32), upper bound
14+
- ๋’ค์ง‘์€ ๋ฌธ์ž์—ด์„ ์ •์ˆ˜๋กœ ๋ณ€ํ™˜ํ•˜๋Š”๋ฐ ๋ฌธ์ž์—ด์— ๋น„๋ก€ํ•˜๋ฉฐ ์ด ๊ธธ์ด๋Š” ์ตœ๋Œ€ 32์ด๋ฏ€๋กœ, O(32), upper bound
15+
> O(32) * O(32) * O(32) * O(32) ~= O(1)
16+
17+
Memory: 16.50 (Beats 64.72%)
18+
Space Complexity: O(1)
19+
- ๊ฐ ๋‹จ๊ณ„๋งˆ๋‹ค ์ตœ๋Œ€ ๊ธธ์ด๊ฐ€ 32์ธ ๋ฌธ์ž์—ด์ด ์ž„์‹œ๋กœ ์ €์žฅ๋˜๋ฏ€๋กœ O(32) * 4
20+
> O(32) * 4 ~= O(1)
21+
"""
22+
def solve(self, n: int) -> int:
23+
return int(str(n).zfill(32)[::-1], 2)
24+
25+
26+
class _LeetCodeTestCases(TestCase):
27+
def test_1(self):
28+
n = int("00000010100101000001111010011100")
29+
output = 964176192
30+
self.assertEqual(Solution.reverseBits(Solution(), n), output)
31+
32+
33+
if __name__ == '__main__':
34+
main()

0 commit comments

Comments
ย (0)