[LeetCode]Implement Stack using Queues
2016-04-02 16:39
555 查看
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.
Notes:
You may assume that all operations are valid (for example, no pop or top operations will be called on an empty stack).
Depending on your language, queue may not be supported natively. You may simulate a queue by using a list or deque (double-ended queue), as long as you use only standard operations of a queue -- which means only
and
Update (2015-06-11):
The class name of the Java function had been updated to MyStack instead of Stack.
Credits:
Special thanks to @jianchao.li.fighter for adding this problem and all test cases.
[LeetCode]
两种实现方法
1)两个队列,当取栈顶时,把全部元素进到还有一个队列,然后最后出队列的就是栈顶。pop的操作也是类似的,注意两个队列之间的转换。
2)一个队列,记录队列此时的长度n,出队列第n次的元素就是栈顶元素。
我们能够把出队列的元素再次入队列,这样就能够用一个队列实现。
同理用两个栈实现队列也是类似的做法。
两个队列实现:
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.
Notes:
You may assume that all operations are valid (for example, no pop or top operations will be called on an empty stack).
Depending on your language, queue may not be supported natively. You may simulate a queue by using a list or deque (double-ended queue), as long as you use only standard operations of a queue -- which means only
push to back,
pop from front,
size,
and
is emptyoperations are valid.
Update (2015-06-11):
The class name of the Java function had been updated to MyStack instead of Stack.
Credits:
Special thanks to @jianchao.li.fighter for adding this problem and all test cases.
[LeetCode]
两种实现方法
1)两个队列,当取栈顶时,把全部元素进到还有一个队列,然后最后出队列的就是栈顶。pop的操作也是类似的,注意两个队列之间的转换。
2)一个队列,记录队列此时的长度n,出队列第n次的元素就是栈顶元素。
我们能够把出队列的元素再次入队列,这样就能够用一个队列实现。
同理用两个栈实现队列也是类似的做法。
两个队列实现:
class Stack { private: queue<int>temp[2]; int cur = 0; public: // Push element x onto stack. void push(int x) { temp[cur].push(x); } // Removes the element on top of the stack. void pop(void) { while(temp[cur].size()>1){ int n = temp[cur].front(); temp[cur].pop(); temp[1-cur].push(n); } temp[cur].pop(); cur = 1-cur; } // Get the top element. int top(void) { while(temp[cur].size()>1){ int n = temp[cur].front(); temp[cur].pop(); temp[1-cur].push(n); } int ret = temp[cur].front(); temp[cur].pop(); temp[1-cur].push(ret); cur = 1- cur; return ret; } // Return whether the stack is empty. bool empty(void) { return temp[cur].empty(); } };一个队列实现:
class Stack { private: queue<int>temp; public: // Push element x onto stack. void push(int x) { temp.push(x); } // Removes the element on top of the stack. void pop(void) { int len = temp.size(); while(len>1){ int n = temp.front(); temp.pop(); temp.push(n); len -- ; } temp.pop(); } // Get the top element. int top(void) { int len = temp.size(); while(len>1){ int n = temp.front(); temp.pop(); temp.push(n); len -- ; } int ret = temp.front(); temp.pop(); temp.push(ret); return ret; } // Return whether the stack is empty. bool empty(void) { return temp.empty(); } };
相关文章推荐
- JS/html/form/XMLHttpRequest Ajax 批量上传文件/图片的3种方式
- POJ1679 The Unique MST
- <%@ include file=” ”%> 和<jsp:include page=” ” flush=”true”/> 区别
- Unique Binary Search Trees
- UITableView的优化
- leetcode 300. Longest Increasing Subsequence 最长上升序列数
- LeetCode334. Increasing Triplet Subsequence
- easyui tree文本单击事件切换展开/折叠节点的状态
- Android多国语言的value文件夹命名方式
- AndroidUI组件之ActionBar--基于下拉的导航方式
- requirejs的使用
- 60. Permutation Sequence
- 这么巧妙思路(二)Longest Ordered Subsequence
- 232. Implement Queue using Stacks
- String,StringBuffer与StringBuilder的区别??
- 免安裝、免設定的 Hadoop 開發環境 - cloudera 的 QuickStart VM
- 最长公共子序列问题 POJ 1458 Common Subsequence
- 这么巧妙的思路Common Subsequence
- Xcode的Architectures、Valid Architectures和Build Active Architecture Only属性
- continue继续循环