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 empty
operations 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 ...
随机推荐
- cocos2d-x学习记录第一篇-环境变量配置
最近准备学习cocos2d-x,之前一直是做iOS开发的,算是零基础开始学习吧. (此条后来修改,不用配置下面这些东西,下载一个cocosstudio就可以了,直接在里边就创建工程了) 本人用Mac电 ...
- ROW_NUMBER()与PARTITION BY 实例
环境:SQL Server 2008 R2 数据表结构 SELECT A.* FROM [tbiz_AssScoreWeidu] A SELECT A.* ,ROW_NUMBER() OVER ( P ...
- socket泄露的问题
在增加keepalive和libevent 超时后,仍然会几个月后出现lsof统计句柄过多的问题,如下图,红线圈定的是不定期出现的错误关闭流,时间点和server端日志(只看到accpet fd,没有 ...
- acm 1002 算法设计
最近突然想往算法方向走走,做了做航电acm的几道题 二话不说,开始 航电acm 1002 题主要是处理长数据的问题,算法原理比较简单,就是用字符数组代替int,因为int太短需要处理的数据较长 下面是 ...
- A页面调到B页面,B页面关闭时A页面刷新
// A.html <html> <head> <script type="text/javascript"> alert("refr ...
- Mac 下locate命令使用问题WARNING: The locate database (/var/db/locate.database) does not exist.
想在Mac下使用locate时,提醒数据库没创建: WARNING: The locate database (/var/db/locate.database) does not exist. To ...
- python shutil.copy()用法
shutil.copyfile(src, dst):复制文件内容(不包含元数据)从src到dst. DST必须是完整的目标文件名; 如果src和dst是同一文件,就会引发错误shutil.Error. ...
- Centos7学习之静态IP设置方法介绍
1.编辑 ifcfg-eth0 文件,vim 最小化安装时没有被安装,需要自行安装不描述. # vim /etc/sysconfig/network-scripts/ifcfg-eth0 2.修改 ...
- SecureCRT在远程主机和本地之间传输文件
概述 Linux系统不像Window系统有清晰的操作页面,数据能Copy下来,远程Linux的数据,就牵扯到本地数据与远程服务器数据交换的问题.Linux远程攻击SecureCrt提供了2种方式,可以 ...
- 表空间统计报告 Tablespace growth Report
SQL> select TS# from v$tablespace where name='ABC' ; TS# ---------- 6 set serverout on set verify ...