-
Notifications
You must be signed in to change notification settings - Fork 125
/
Copy pathChaedie.py
50 lines (42 loc) Β· 1.2 KB
/
Chaedie.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
"""
1. νμ΅μ μ§ννμ§λ§, μ€μ€λ‘ μλ²½νκ² νμ§ λͺ»νμ΅λλ€.
λ€μμ£Όμ bit μ°μ°μΌλ‘ λ€μ νμ΄λ³Ό μμ μ
λλ€.
"""
class Solution:
# μκ³ λ¬λ νμ΄ 1) Stack
def reverseBits(self, n: int) -> int:
stack = []
while len(stack) < 32:
stack.append(n % 2)
n //= 2
result, scale = 0, 1
while stack:
result += stack.pop() * scale
scale *= 2
return result
# μκ³ λ¬λ νμ΄ 2) bit manipulation
def reverseBits(self, n: int) -> int:
result = 0
print(n)
for i in range(32):
print(result)
result <<= 1
result |= n & 1
n >>= 1
return result
# NeetCode νμ΄
def reverseBits(self, n: int) -> int:
res = 0
for i in range(32):
bit = (n >> i) & 1
res = res | (bit << (31 - i))
return res
# μ€μ€λ‘ νκΈ°
# νλ² λ ν μμ μ
λλ€.
def reverseBits(self, n: int) -> int:
result = 0
for i in range(32):
result = result << 1
result = result | (n & 1)
n = n >> 1
return result