Skip to content

Latest commit

 

History

History
24 lines (21 loc) · 712 Bytes

225.md

File metadata and controls

24 lines (21 loc) · 712 Bytes

LeetCode 225 Implement Stack using Queues——easy

Implement the following operations of a stack using queues.
push(x) -- Push element x onto stack.
pop() -- Removes the element on top of the stack.
top() -- Get the top element.
empty() -- Return whether the stack is empty.
Example:
MyStack stack = new MyStack();

stack.push(1);
stack.push(2);  
stack.top();   // returns 2
stack.pop();   // returns 2
stack.empty(); // returns false

思路:
1、用队列来实现栈
2、每次push一个数、便逆序删除添加n-1次
3、其余操作直接用