LeetCode232: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, and is 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).
使用栈来实现一个队列。这个题目之前在《剑指offer》上面见过,没有什么好说的。使用两个栈,一个栈用来保存插入的元素。另外一个栈用来运行pop或top操作,每当运行pop或top操作时检查另外一个栈是否为空,假设为空,将第一栈中的元素所有弹出并插入到第二个栈中。再将第二个栈中的元素弹出就可以。须要注意的是这道题的编程时有一个技巧,能够使用peek来实现pop。这样能够降低反复代码。编程时要能扩展思维,假设因为pop函数的声明再前面就陷入用pop来实现peek的功能的话就会感觉无从下手了。
runtime:0ms
class Queue {
public:
// Push element x to the back of queue.
void push(int x) {
pushStack.push(x);
}
// Removes the element from in front of queue.
void pop(void) {
peek();//这里能够使用peek进行两个栈之间元素的转移从而避免反复代码
popStack.pop();
}
// Get the front element.
int peek(void) {
if(popStack.empty())
{
while(!pushStack.empty())
{
popStack.push(pushStack.top());
pushStack.pop();
}
}
return popStack.top();
}
// Return whether the queue is empty.
bool empty(void) {
return pushStack.empty()&&popStack.empty();
}
private:
stack<int> pushStack;//数据被插入到这个栈中
stack<int> popStack;//数据从这个栈中弹出
};
LeetCode232:Implement Queue using Stacks的更多相关文章
- LeetCode232 Implement Queue using Stacks Java 题解
题目: Implement the following operations of a queue using stacks. push(x) -- Push element x to the bac ...
- 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( ...
- 232. Implement Queue using Stacks,225. Implement Stack using Queues
232. Implement Queue using Stacks Total Accepted: 27024 Total Submissions: 79793 Difficulty: Easy Im ...
- 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 ...
- Lintcode: Implement Queue by Stacks 解题报告
Implement Queue by Stacks 原题链接 : http://lintcode.com/zh-cn/problem/implement-queue-by-stacks/# As th ...
- Leetcode 232 Implement Queue using Stacks 和 231 Power of Two
1. 232 Implement Queue using Stacks 1.1 问题描写叙述 使用栈模拟实现队列.模拟实现例如以下操作: push(x). 将元素x放入队尾. pop(). 移除队首元 ...
- LeetCode 232. 用栈实现队列(Implement Queue using Stacks) 4
232. 用栈实现队列 232. Implement Queue using Stacks 题目描述 使用栈实现队列的下列操作: push(x) -- 将一个元素放入队列的尾部. pop() -- 从 ...
- [Swift]LeetCode232. 用栈实现队列 | Implement Queue using Stacks
Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of ...
随机推荐
- Unix系统编程()main函数的命令行参数
命令行参数输入双引号是什么效果? 好像可以去空格化.
- ARM与X86架构的对决[整编]
CISC(复杂指令集计算机)和RISC(精简指令集计算机)是当前CPU的两种架构.它们的区别在于不同的CPU设计理念和方法.早期的CPU全部是CISC架构,它的设计目的是 CISC要用最少的机器语言 ...
- redis-trib构建集群
https://blog.csdn.net/qq_35946990/article/details/78957618
- php 区分中文,英文,中英混合
$str1="是你"; $strA = trim($str1); $lenA = strlen($strA); $lenB = mb_strlen($strA,"utf- ...
- Easyui控制combotree只能选择叶子节点
$(function() { $('#tt').combotree({ url: 'getTree.do', onBeforeSelect: function(node) { if (!$(this) ...
- Django - 安装filebrowser发生Error finding Upload-Folder错误
Error finding Upload-Folder (site.storage.location + site.directory). Maybe it does not exist? 解决: F ...
- C#从Excel中读取数据为空
将HDR设置为YES,IMEX设置为1即可. OleDbConnection objConn = new OleDbConnection("Provider=Microsoft.ACE.OL ...
- SSH框架-Struts2基础-Action
Struts2的目录结构: 解压apps目录下的struts2-blank.war: 仿照这个最基本的项目,拷贝相关文件: 1.拷贝apps/struts2-blank/WEB-INF/classes ...
- ThreadLocal分析
我们再介绍一个在多线程环境中经常使用的类ThreadLocal,它是java为解决多线程程序的并发问题提供了一种新的方向,使用这个ThreadLocal类可以帮助开发者很简单地编写出简洁的程序,并且是 ...
- git base 简单命令行
记录几个简单的命令 1:克隆-把线上的文件复制到本地 git clone 线上地址 2:检查状态 git status 3:放入待仓储 git add.文件名 git add * (全部文件,简单粗暴 ...