# SE103 Week 2 Post Session Solutions [Implement Queue Using Stacks](#Implement-Queue-Using-Stacks) TODO: Validate Stack Sequences TODO: Sum of Subarray Minimums ## Implement Queue Using Stacks **Java** ```java class MyQueue { Stack<Integer> input = new Stack(); Stack<Integer> output = new Stack(); public void push(int x) { input.push(x); } public void pop() { peek(); output.pop(); } public int peek() { if (output.empty()) while (!input.empty()) output.push(input.pop()); return output.peek(); } public boolean empty() { return input.empty() && output.empty(); } } ``` **Python** ```python class Queue(object): def __init__(self): self.inStack, self.outStack = [], [] def push(self, x): self.inStack.append(x) def pop(self): self.move() self.outStack.pop() def peek(self): self.move() return self.outStack[-1] def empty(self): return (not self.inStack) and (not self.outStack) def move(self): if not self.outStack: while self.inStack: self.outStack.append(self.inStack.pop()) ```
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up