# 232. Implement Queue using Stacks https://leetcode.com/problems/implement-queue-using-stacks/ ## 思路: 1. 不太清楚網友的解法跟自己的差別,雖然我用pop(0)也是O(n) 但是他直接用for loop 也是O(n)阿 ## 我自己的解法 ```python= class MyQueue: def __init__(self): self.list=[] def push(self, x: int) -> None: self.list.append(x) def pop(self) -> int: return self.list.pop(0) def peek(self) -> int: return self.list[0] def empty(self) -> bool: if not self.list : return True else : 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() ``` ## 網友的解法 ```python= class MyQueue(object): def __init__(self): self.in_stk = [] self.out_stk = [] # Push element x to the back of queue... def push(self, x): self.in_stk.append(x) # Remove the element from the front of the queue and returns it... def pop(self): self.peek() return self.out_stk.pop() # Get the front element... def peek(self): if not self.out_stk: while self.in_stk: self.out_stk.append(self.in_stk.pop()) return self.out_stk[-1] # Return whether the queue is empty... def empty(self): return not self.in_stk and not self.out_stk ```