[LeetCode]94. Implement Queue using Stacks用栈实现队列
2015-11-19 10:31
417 查看
Implement the following operations of a queue using stacks.
push(x) -- Push element x to the back of queue.
pop() -- Removes the element from in front of queue.
peek() -- Get the front element.
empty() -- Return whether the queue is empty.
Notes:
You must use only standard operations of a stack -- which means only
Depending on your language, stack may not be supported natively. You may simulate a stack by using a list or deque (double-ended queue), as long as you use only standard operations of a stack.
You may assume that all operations are valid (for example, no pop or peek operations will be called on an empty queue).
Subscribe to see which companies asked this question
解法1:使用两个栈s1和s2。进队列调用s1.push();因为队列的先进先出特性,出队列不能直接在s1上操作,我们用辅助栈s2来操作:如果s2非空,则直接s2.pop(),否则先将s1中的所有元素倒腾到s2中,再调用s2.pop();同理对于取队列头元素也是相同做法;判断是否为空则只要判断s1和s2是否都是空的就可以了。注意题目限制了假设所有的操作都是有效的,所以在pop()和peek()两个操作上不需要做额外的非空判断。
解法2:使用一个栈s即可解决。在进行push()操作的时候,如果s中有元素,则将它们搬到辅助栈tmp中,然后再将x push到s中;接下来又将tmp中的所有元素搬回s中。这样最先进入的元素就在栈顶了。
实际上这种方法还是使用到了一个辅助栈,并且在入栈操作上需要循环多次,所以效率更低,但是空间复杂度不见得下降了。
push(x) -- Push element x to the back of queue.
pop() -- Removes the element from in front of queue.
peek() -- Get the front element.
empty() -- Return whether the queue is empty.
Notes:
You must use only standard operations of a stack -- which means only
push to top,
peek/pop from top,
size, and
is emptyoperations are valid.
Depending on your language, stack may not be supported natively. You may simulate a stack by using a list or deque (double-ended queue), as long as you use only standard operations of a stack.
You may assume that all operations are valid (for example, no pop or peek operations will be called on an empty queue).
Subscribe to see which companies asked this question
解法1:使用两个栈s1和s2。进队列调用s1.push();因为队列的先进先出特性,出队列不能直接在s1上操作,我们用辅助栈s2来操作:如果s2非空,则直接s2.pop(),否则先将s1中的所有元素倒腾到s2中,再调用s2.pop();同理对于取队列头元素也是相同做法;判断是否为空则只要判断s1和s2是否都是空的就可以了。注意题目限制了假设所有的操作都是有效的,所以在pop()和peek()两个操作上不需要做额外的非空判断。
class Queue { public: // Push element x to the back of queue. void push(int x) { s1.push(x); } // Removes the element from in front of queue. void pop(void) { if (s2.empty() && !s1.empty()) { while (!s1.empty()) { s2.push(s1.top()); s1.pop(); } } s2.pop(); } // Get the front element. int peek(void) { if (s2.empty() && !s1.empty()) { while (!s1.empty()) { s2.push(s1.top()); s1.pop(); } } return s2.top(); } // Return whether the queue is empty. bool empty(void) { return s1.empty() && s2.empty(); } private: std::stack<int> s1, s2; };
解法2:使用一个栈s即可解决。在进行push()操作的时候,如果s中有元素,则将它们搬到辅助栈tmp中,然后再将x push到s中;接下来又将tmp中的所有元素搬回s中。这样最先进入的元素就在栈顶了。
class Queue { public: // Push element x to the back of queue. void push(int x) { std::stack<int> tmp; while (!s.empty()) { tmp.push(s.top()); s.pop(); } s.push(x); while (!tmp.empty()) { s.push(tmp.top()); tmp.pop(); } } // Removes the element from in front of queue. void pop(void) { s.pop(); } // Get the front element. int peek(void) { return s.top(); } // Return whether the queue is empty. bool empty(void) { return s.empty(); } private: std::stack<int> s; };
实际上这种方法还是使用到了一个辅助栈,并且在入栈操作上需要循环多次,所以效率更低,但是空间复杂度不见得下降了。
相关文章推荐
- Android UI最佳实践
- Unable to find valid certification path to requested target
- 淘宝前端框架kissyui
- iOS UIAutomation中alert处理方法
- 关于UIScrollView属性和方法的总结
- 详解iOS开发中的转场动画和组动画以及UIView封装动画
- 深入分析Java使用+和StringBuilder进行字符串拼接的差异
- Kendo UI - Observable
- 知名界面开发控件Kendo UI Professional Q3 2015 SP1正式发布[附下载]
- UVa 1584 - Circular Sequence
- Kibana User Guide [4.2] » Getting Started with Kibana » Data Visualization: Beyond Discovery
- 设计模式:Java 建造者模式(Builder)
- Android中build target,minSdkVersion,targetSdkVersion,maxSdkVersion概念区分
- Android关于buildToolVersion与CompileSdkVersion的区别
- EasyUI Datagrid 鼠标悬停显示单元格内容 复制代码
- EasyUI Datagrid 鼠标悬停显示单元格内容
- UICollectionView 使用 介绍
- GUI for git|SourceTree|入门基础
- UI课程用for循环写n*n棋盘
- 过来人经验!工作五年以上的UI设计师都在干什么?