← 返回 amazon 的题目列表Design a Stack with Reverse Function
类型:online_judge
amazon
Design a stack with a reverse function. Support the following operations:
push(x): Push element x onto stack.
pop(): Removes the element on top of the stack.
top(): Get the top element.
reverse(): Reverse the stack, making the bottom element the top and the top element the bottom.
Implement these operations, ensuring the time complexity of each operation is as low as possible. Implement O(1) reverse operation if possible. Provide a complete code implementation.
Input/Output Explanation
Input a sequence of stack operations in order and output the result of each operation.
Example
Input:
push 1
push 2
push 3
reverse
top
pop
top
Output:
3
1
2
Constraints
The maximum number of operations is 1000.
Elements in the stack are integers ranging from [-1000, 1000].
Example
Input
push 1
push 2
push 3
reverse
top