Leetcode 232 Implement Queue using Stacks STL
本题用两个栈实现队列,用栈的基本操作去实现队列的所有基本操作push(),pop(),peek()以及empty()
sa作为输入栈,sb作为输出栈,将sa输入元素的反转过来放到sb中
push与sa有关,而pop(),peek()与sb有关,即将sa输入元素出栈放到sb中(函数move).
为此,只有两个栈为空才能让队列为空
class Queue {
public:
// Push element x to the back of queue.
stack<int> sa;
stack<int> sb;
void move(){
while(!sa.empty()){
sb.push(sa.top());
sa.pop();
}
}
void push(int x) {
sa.push(x);
}
// Removes the element from in front of queue.
void pop(void) {
if(!sb.empty()) sb.pop();
else{
move();
sb.pop();
}
}
// Get the front element.
int peek(void) {
if(!sb.empty()) return sb.top();
else{
move();
return sb.top();
}
}
// Return whether the queue is empty.
bool empty(void) {
return sa.empty() && sb.empty();
}
};
Leetcode 232 Implement Queue using Stacks STL的更多相关文章
- 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 用栈来实现队列
Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of ...
- (easy)LeetCode 232.Implement Queue using Stacks
Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of ...
- Java [Leetcode 232]Implement Queue using Stacks
题目描述: Implement the following operations of a queue using stacks. push(x) -- Push element x to the b ...
- LeetCode 232 Implement Queue using Stacks
Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of ...
- Java for LeetCode 232 Implement Queue using Stacks
Stack<Integer> stack=new Stack<Integer>(); public void push(int x) { stack.push(x); } // ...
- LeetCode 232 Implement Queue using Stacks 两个栈实现队列
class MyQueue { public: /** Initialize your data structure here. */ MyQueue() { } /** Push element x ...
- 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 ...
- 232. Implement Queue using Stacks,225. Implement Stack using Queues
232. Implement Queue using Stacks Total Accepted: 27024 Total Submissions: 79793 Difficulty: Easy Im ...
随机推荐
- [aspx]控件及代码小例
1. 原生 asp 方式遍历 DataTable 2. aspx 的控件 Repeater 后台绑定 <%-- DataTable dt = OleDbHelper.GetTable(&quo ...
- Xcode 设置代码不自动换行
"command + ,"打开设置界面后,找到"Text Editing" 然后选择"Indentation", 最后找到"lin ...
- Java获取服务器网址
StringBuffer url1 = request.getRequestURL(); String tempContextUrl1 = url1.delete(url1.length() - re ...
- 問題排查:.NETSystem.Runtime.Remoting.RemotingException: TCP 信道协议冲突: 应为报头。
這個錯誤訊息是在一個 Web Serveice 的偵錯階段發生的 目前還未找到原因,環境如下: 作業系統:Windows 10 x64 企業版 (簡中) 開發工具:Visual Studio 2013 ...
- 云计算和大数据时代网络技术揭秘(十二)自定义网络SDN
软件定义网络——SDN SDN是网络技术热点,即软件定义网络,OpenFlow是实现SDN思想的一个框架标准, open是指公开.开放,具体为控制平面的规则由各个通信厂家自定义变为公开的技术标准, f ...
- EasyUI Combobox设定默认值
$(function () { $('#Select6').combobox({ onLoadSuccess: function () { var data = $('#Select6').combo ...
- Points on cycle
Description There is a cycle with its center on the origin. Now give you a point on the cycle, you a ...
- python基础语法(4)
十.Python标准库 Python标准库是随Pthon附带安装的,包含了大量极其有用的模块. 1. sys模块 sys模块包含系统对应的功能 sys.argv ---包含命令行参数,第一个参数是py ...
- Node.js-提供了四种形式的定时器
Node.js提供了四种形式的定时器 global.setTimeout(); //一次性定时器 global.setInterval(); //周期性定时器 global.nextTick(); / ...
- DBAPI部署
1.添加源 sudo rpm -Uvh http://download.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm ...