File tree 1 file changed +16
-12
lines changed
1 file changed +16
-12
lines changed Original file line number Diff line number Diff line change @@ -4,43 +4,47 @@ def __init__(self):
4
4
"""
5
5
Initialize your data structure here.
6
6
"""
7
- self .s1 = list ()
8
- self .s2 = list ()
7
+ self .stack1 = []
8
+ self .stack2 = []
9
9
10
-
11
10
def push (self , x ):
12
11
"""
13
12
Push element x to the back of queue.
14
13
:type x: int
15
14
:rtype: None
16
15
"""
17
- self .s1 .append (x )
18
- # for i in range(len(s1)):
19
- while (self .s1 ):
20
- self .s2 .append (self .s1 .pop ())
21
-
16
+ self .stack1 .append (x )
22
17
23
18
def pop (self ):
24
19
"""
25
20
Removes the element from in front of queue and returns that element.
26
21
:rtype: int
27
22
"""
28
- return self .s2 .pop (0 )
29
-
23
+ if not self .stack2 :
24
+ while self .stack1 :
25
+ tmp = self .stack1 .pop ()
26
+ self .stack2 .append (tmp )
27
+ res = self .stack2 .pop ()
28
+ return res
30
29
31
30
def peek (self ):
32
31
"""
33
32
Get the front element.
34
33
:rtype: int
35
34
"""
36
- return self .s2 [0 ]
35
+ # print self.stack1, self.stack2
36
+ if not self .stack2 :
37
+ while self .stack1 :
38
+ tmp = self .stack1 .pop ()
39
+ self .stack2 .append (tmp )
40
+ return self .stack2 [- 1 ]
37
41
38
42
def empty (self ):
39
43
"""
40
44
Returns whether the queue is empty.
41
45
:rtype: bool
42
46
"""
43
- return len ( self .s2 ) == 0
47
+ return not self .stack1 and not self . stack2
44
48
45
49
46
50
# Your MyQueue object will be instantiated and called as such:
You can’t perform that action at this time.
0 commit comments