forked from DaleStudy/leetcode-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathseungriyou.py
More file actions
32 lines (28 loc) · 825 Bytes
/
seungriyou.py
File metadata and controls
32 lines (28 loc) · 825 Bytes
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
# https://leetcode.com/problems/reverse-bits/
class Solution:
def reverseBits_32(self, n: int) -> int:
"""
[Complexity]
- TC: O(32)
- SC: O(1)
[Approach]
n의 맨 오른쪽 bit부터 res의 맨 왼쪽에 붙여나가기
"""
res = 0
for i in range(32):
res |= ((n >> i) & 1) << (31 - i)
return res
def reverseBits(self, n: int) -> int:
"""
[Complexity]
- TC: O(16)
- SC: O(1)
[Approach]
n의 바깥쪽에서부터 two pointer 처럼 res에 모으기
"""
res = 0
for i in range(16):
left = (n >> (31 - i)) & 1
right = (n >> i) & 1
res |= (left << i) | (right << (31 - i))
return res