-
Notifications
You must be signed in to change notification settings - Fork 1
/
implement_queue.py
94 lines (77 loc) · 1.98 KB
/
implement_queue.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#coding=utf-8
"""
232. Implement Queue using Stacks
"""
class MyQueue(object):
def __init__(self):
"""
Initialize your data structure here.
"""
# list的行为更像是stack
self.value = []
def push(self, x):
"""
Push element x to the back of queue.
:type x: int
:rtype: void
"""
self.value.append(x)
def pop(self):
"""
Removes the element from in front of queue and returns that element.
:rtype: int
"""
if not self.value:
return
s = []
while len(self.value) > 1:
s.append(self.value.pop())
peek = self.value.pop()
while s:
self.value.append(s.pop())
return peek
def peek(self):
"""
Get the front element.
:rtype: int
"""
s = []
while self.value:
s.append(self.value.pop())
p = s[-1]
while s:
self.value.append(s.pop())
return p
def empty(self):
"""
Returns whether the queue is empty.
:rtype: bool
"""
return self.value == []
class MyQueue1(object):
"""
看了讨论区其他解法,觉得初始化用2个栈更方便,如果有连续的pop操作效率更高,不需要重新push到原来的栈
"""
def __init__(self):
self.stack1 = []
self.stack2 = []
def push(self, x):
self.stack1.append(x)
def pop(self):
self.peek()
return self.stack2.pop()
def peek(self):
if self.empty():
return
if not self.stack2:
while self.stack1:
self.stack2.append(self.stack1.pop())
return self.stack2[-1]
def empty(self):
return not self.stack1 and self.stack2
# Your MyQueue object will be instantiated and called as such:
# obj = MyQueue()
# obj.push(x)
# param_2 = obj.pop()
# param_3 = obj.peek()
# param_4 = obj.empty()