forked from super30admin/Design-2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem1.py
More file actions
51 lines (42 loc) · 1.15 KB
/
Problem1.py
File metadata and controls
51 lines (42 loc) · 1.15 KB
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
class MyQueue(object):
def __init__(self):
self.stack= []
self.q_stack= []
def push(self, x):
"""
Append X.
:type x: int
:rtype: None
"""
self.stack.append(x)
return
def pop(self):
"""
:rtype: int
"""
self.peek()
return self.q_stack.pop()
def peek(self):
"""
Return element at 0th index.
:rtype: int
"""
if not self.q_stack:
# If q_stack is empty append all the elements.
while self.stack:
self.q_stack.append(self.stack.pop())
# If q_stack has elements, then return the last element in q_stack.
return self.q_stack[len(self.q_stack)-1]
def empty(self): # O(1)
"""
:rtype: bool
"""
if not self.stack and not self.q_stack:
return True
return False
# 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()