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 ...
随机推荐
- 复选框css
input, select, button, textarea{ -webkit-appearance:none; }该属性会导致复选框失去选择效果
- word20161219
Remote Installation Services / 远程安装服务 remote procedure call, RPC / 远程过程调用 remote storage / 远程存储 Remo ...
- centos 7 配置网络
文档: https://wiki.centos.org/FAQ/CentOS7
- Tomcat的设定
tomcat 版本 apache-tomcat-7.0.68-windows-x64 1.解压文件到 eclipse文件夹中,这个放哪都可以,个人习惯而已 2.tomcat目录结构 图片为盗图- 3 ...
- js日期相关
时间戳转正常日期时间 1469512964000 —> 2016/7/26 下午2:02 var getLocalTime = function(nS) { // 13位时间戳 return n ...
- python class metaclass instance
>>> class CObj(object):... pass...>>> dir()['CObj', '__builtins__', '__doc__', '__ ...
- sort()基础知识总结+超简短的英文名排序写法
结合前些天学的箭头函数我想到一种非常简短的sort排序写法:(这可能是最短的英文名排序方法了) 贴出来大家一起探讨一下: [4,1,2,32].sort((x,y)=>x>y); //[1 ...
- magento后台使用POST表单时,要使用必要参数form_key才能正常通讯
<form action="<?php echo $this->getSaveUrl() ?>" method="POST" encty ...
- urlencode遇到中文编码问题
urlencode并不会改变输入的编码格式, 默认会将中文输出为 gbk 编码, 类似的, quote 会对中文进行 gbk 编码 不过, 当遇到嵌套多层的字典时, 问题就来了, 中文会被 utf8 ...
- STM32F412应用开发笔记之一:初识NUCLEO-F412ZG
今天终于收到了期待已久的NUCLEO-F412ZG,感谢电子发烧友论坛! 近几年来基本都是在STM32平台上做一些设计开发工作.STM32F103.STM32F107.STM32F429等都应用过,但 ...