We’re preparing your current view and syncing the latest data.
Implement a first in first out (FIFO) queue using only two stacks. The implemented queue should support all the functions of a normal queue (push, pop, peek, empty).
Implement the MyQueue class:
Notes:
Example: MyQueue queue = new MyQueue(); queue.push(1); queue.push(2); queue.peek(); // Returns 1 queue.pop(); // Returns 1 queue.empty(); // Returns false
For method calls and parameters, the format depends on the testing environment. Typically, you will be given a sequence of method calls such as push(x), pop(), peek(), empty()
Each call to pop() or peek() returns an integer; calls to empty() return a boolean.
1 <= x <= 9 At most 100 calls will be made to push, pop, peek, and empty. All calls to pop and peek are valid (i.e., the queue is not empty when these calls are made).
Example 1
Input
["MyQueue", "push", "push", "peek", "pop", "empty"], [[], [1], [2], [], [], []]
Output
[null, null, null, 1, 1, false]
Explanation
We initialize the queue, push 1 and 2 into it, peek returns 1 which is the front element, pop removes and returns 1, empty checks if queue is empty returning false.