forked from super30admin/Design-2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem_1.py
More file actions
34 lines (25 loc) · 804 Bytes
/
Copy pathProblem_1.py
File metadata and controls
34 lines (25 loc) · 804 Bytes
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
#Time Complexity : Ammortized O(1)
#Space Complexity : O(n)
#Did this code successfully run on Leetcode : Yes
#Any problem you faced while coding this : No
class MyQueue(object):
def __init__(self):
self.in_st = []
self.out_st = []
def push(self, x):
self.in_st.append(x)
def pop(self):
if not self.out_st:
while self.in_st:
self.out_st.append(self.in_st.pop())
return self.out_st.pop()
def peek(self):
if not self.out_st:
while self.in_st:
self.out_st.append(self.in_st.pop())
return self.out_st[-1]
def empty(self):
if not self.in_st:
if not self.out_st:
return True
return False