File tree Expand file tree Collapse file tree 1 file changed +50
-0
lines changed Expand file tree Collapse file tree 1 file changed +50
-0
lines changed Original file line number Diff line number Diff line change
1
+ """
2
+ 1. ํ์ต์ ์งํํ์ง๋ง, ์ค์ค๋ก ์๋ฒฝํ๊ฒ ํ์ง ๋ชปํ์ต๋๋ค.
3
+ ๋ค์์ฃผ์ bit ์ฐ์ฐ์ผ๋ก ๋ค์ ํ์ด๋ณผ ์์ ์
๋๋ค.
4
+ """
5
+
6
+
7
+ class Solution :
8
+
9
+ # ์๊ณ ๋ฌ๋ ํ์ด 1) Stack
10
+ def reverseBits (self , n : int ) -> int :
11
+ stack = []
12
+ while len (stack ) < 32 :
13
+ stack .append (n % 2 )
14
+ n //= 2
15
+
16
+ result , scale = 0 , 1
17
+ while stack :
18
+ result += stack .pop () * scale
19
+ scale *= 2
20
+ return result
21
+
22
+ # ์๊ณ ๋ฌ๋ ํ์ด 2) bit manipulation
23
+ def reverseBits (self , n : int ) -> int :
24
+ result = 0
25
+ print (n )
26
+ for i in range (32 ):
27
+ print (result )
28
+ result <<= 1
29
+ result |= n & 1
30
+ n >>= 1
31
+ return result
32
+
33
+ # NeetCode ํ์ด
34
+ def reverseBits (self , n : int ) -> int :
35
+ res = 0
36
+
37
+ for i in range (32 ):
38
+ bit = (n >> i ) & 1
39
+ res = res | (bit << (31 - i ))
40
+ return res
41
+
42
+ # ์ค์ค๋ก ํ๊ธฐ
43
+ # ํ๋ฒ ๋ ํ ์์ ์
๋๋ค.
44
+ def reverseBits (self , n : int ) -> int :
45
+ result = 0
46
+ for i in range (32 ):
47
+ result = result << 1
48
+ result = result | (n & 1 )
49
+ n = n >> 1
50
+ return result
You canโt perform that action at this time.
0 commit comments