Leetcode Implement Queue using Stacks
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
push to top,peek/pop from top,size, andis 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).
题目意思:
用栈实现队列
解题思路:
用两个栈实现,一个栈用作队列的出口,一个栈用作队列的进口
扩展:
c++ 的STL中队列的实现不是用的栈,其实现原理是开辟一段内存,该内存中每个节点指向一段连续的内存空间,然后维护这些结点,具体参考《STL源码剖析》,面试时可能会问到STL队列的实现方式。STL中还有个优先权队列,其实现原理是用的堆数据结构实现的,堆排序算法最好自己能写。
源代码:
class Queue{
stack<int> inStack, outStack;
public:
void push(int x){
inStack.push(x);
}
void pop(void){
if(outStack.empty()){
while(!inStack.empty()){
outStack.push(inStack.top());
inStack.pop();
}
}
outStack.pop();
}
int peek(void){
if(outStack.empty()){
while(!inStack.empty()){
outStack.push(inStack.top());
inStack.pop();
}
}
return outStack.top();
}
bool empty(void) {
return inStack.empty() && outStack.empty();
}
};
Leetcode Implement Queue using Stacks的更多相关文章
- [LeetCode] Implement Queue using Stacks 用栈来实现队列
Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of ...
- LeetCode——Implement Queue using Stacks
Description: Implement the following operations of a queue using stacks. push(x) -- Push element x t ...
- LeetCode Implement Queue using Stacks (数据结构)
题意: 用栈来实现队列. 思路: 一个栈是不够的,至少要两个. (1)插入.永远只插入到stack1中(插到栈顶). (2)弹出.如果stack2不为空,直接弹出stack2的栈顶,否则,将stack ...
- LeetCode 232. 用栈实现队列(Implement Queue using Stacks) 4
232. 用栈实现队列 232. Implement Queue using Stacks 题目描述 使用栈实现队列的下列操作: push(x) -- 将一个元素放入队列的尾部. pop() -- 从 ...
- leetcode:Implement Stack using Queues 与 Implement Queue using Stacks
一.Implement Stack using Queues Implement the following operations of a stack using queues. push(x) - ...
- 【LeetCode】232 & 225 - Implement Queue using Stacks & Implement Stack using Queues
232 - Implement Queue using Stacks Implement the following operations of a queue using stacks. push( ...
- leetcode 155. Min Stack 、232. Implement Queue using Stacks 、225. Implement Stack using Queues
155. Min Stack class MinStack { public: /** initialize your data structure here. */ MinStack() { } v ...
- Leetcode 232 Implement Queue using Stacks 和 231 Power of Two
1. 232 Implement Queue using Stacks 1.1 问题描写叙述 使用栈模拟实现队列.模拟实现例如以下操作: push(x). 将元素x放入队尾. pop(). 移除队首元 ...
- 232. Implement Queue using Stacks,225. Implement Stack using Queues
232. Implement Queue using Stacks Total Accepted: 27024 Total Submissions: 79793 Difficulty: Easy Im ...
随机推荐
- linux第二天
由于今天公司事情比较多,今天基本没有自学.了解了一下type命令:主要是显示命令类型的命令.linux命令分为内置命令和外部命令两种.内置命令就shell内置的命令,外部命令,就是需要有系统存在一个名 ...
- Qt-为应用程序添加logo
在Qt Creator中新建Qt Resource File,名字为logo.qrc 1.选择Add Prefix得到/new/prefix1 2.然后Add Files,将文件添加进去,如CA-DC ...
- HTML 接收本地文件
HTML代码请把文件拖到下面的框里触发drop事件读取拖放的文件常用情况:结合XMLHttpRequest和拖放文件实现上传查看和管理本地文件和图片 <!DOCTYPE HTML> < ...
- 各大浏览器内核特性及对应的Browserhacks举例
1.浏览器内核指的是什么? 简化的浏览器=用户界面+渲染引擎+js解析引擎+数据存储+网络部件 而通常所说的浏览器内核指的是页面渲染引擎(rendering engine). 2.渲染引擎 The r ...
- CentOS7 睡眠 休眠 关机 电源
设置装有 CentOS7 的笔记本合盖后黑屏进入睡眠模式 systemd 能够处理某些电源相关的 ACPI事件,你可以通过从 /etc/systemd/logind.conf 以下选项进行配置: Ha ...
- c/c++ string
string类的定义.操作. #include<iostream> #include<string> using namespace std; int main() { // ...
- android Acitivity之间的几种传值方式(^_^)
对于开发app 来说,数据的传递肯定是少不了的啦,其实app 的本质就是用来呈现数据的. 好的 方式一 Intent.putExtra(TAG,DATA); 应用场景 对于传送单一数据,而又只在 ...
- 关于mysql MYISAM引擎的锁问题
CREATE TABLE `citymy` ( `city_id` smallint(5) unsigned NOT NULL DEFAULT '0', `city` varchar(50) CHAR ...
- Vue.js 快速入门
什么是Vue.js vue是法语中视图的意思,Vue.js是一个轻巧.高性能.可组件化的MVVM库,同时拥有非常容易上手的API.作者是尤雨溪,写下这篇文章时vue.js版本为1.0.7 准备 我推荐 ...
- js与jq对数组的操作
一.数组处理 1.数组的创建 var arrayObj = new Array(); //创建一个数组 var arrayObj = new Array([size]); //创建一个数组并指定长 ...