← 返回 goldmansachs 的题目列表Implement Queue using Two Stacks
类型:online_judge
Implement a queue using two stacks. In the initial implementation, the time complexity for each enqueue and dequeue operation is O(n). Then, improve your implementation to make the time complexity O(1) for enqueue and dequeue operations.
Example:
Input:
5
push 1
push 2
pop
push 3
pop
Output:
1
2
Constraints:
The number of operations does not exceed 1000.
All input integers are integers.
Example
Input
5
push 1
push 2
pop
push 3
pop