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() {
}
void push(int x) {
if(s1.empty() && s2.empty()){
s1.push(x);
s2.push(x);
}
else{
if(x < s2.top()){
s1.push(x);
s2.push(x);
}
else{
s1.push(x);
s2.push(s2.top());
}
}
}
void pop() {
s1.pop();
s2.pop();
return;
}
int top() {
return s1.top();
}
int getMin() {
return s2.top();
}
stack<int> s1,s2;
};
232. Implement Queue using Stacks
class MyQueue {
public:
/** Initialize your data structure here. */
MyQueue() {
}
/** Push element x to the back of queue. */
void push(int x) {
s1.push(x);
}
/** Removes the element from in front of queue and returns that element. */
int pop() {
if(s2.empty())
shiftstack();
int tmp = s2.top();
s2.pop();
return tmp;
}
/** Get the front element. */
int peek() {
if(s2.empty())
shiftstack();
return s2.top();
}
/** Returns whether the queue is empty. */
bool empty() {
return s1.empty() && s2.empty() ? true : false;
}
void shiftstack(){
if(s1.empty())
return;
while(!s1.empty()){
int tmp = s1.top();
s1.pop();
s2.push(tmp);
}
return;
}
stack<int> s1,s2;
};
225. Implement Stack using Queues
将存储的队列之前的数值再次加入到队列的末尾
class MyStack {
public:
/** Initialize your data structure here. */
MyStack() {
}
/** Push element x onto stack. */
void push(int x) {
q.push(x);
for(int i = ;i < q.size() - ;i++){
int tmp = q.front();
q.pop();
q.push(tmp);
}
return;
}
/** Removes the element on top of the stack and returns that element. */
int pop() {
int tmp = q.front();
q.pop();
return tmp;
}
/** Get the top element. */
int top() {
return q.front();
}
/** Returns whether the stack is empty. */
bool empty() {
return q.empty();
}
queue<int> q;
};
http://www.cnblogs.com/grandyang/p/4568796.html
leetcode 155. Min Stack 、232. Implement Queue using Stacks 、225. Implement Stack using Queues的更多相关文章
- 【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(最小栈)
翻译 设计支持push.pop.top和在常量时间内检索最小元素的栈. push(x) -- 推送元素X进栈 pop() -- 移除栈顶元素 top() -- 得到栈顶元素 getMin() -- 检 ...
- 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) - ...
- 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 232 Implement Queue using Stacks 和 231 Power of Two
1. 232 Implement Queue using Stacks 1.1 问题描写叙述 使用栈模拟实现队列.模拟实现例如以下操作: push(x). 将元素x放入队尾. pop(). 移除队首元 ...
- Lintcode: Implement Queue by Stacks 解题报告
Implement Queue by Stacks 原题链接 : http://lintcode.com/zh-cn/problem/implement-queue-by-stacks/# As th ...
- 【LeetCode OJ 232】Implement Queue using Stacks
题目链接:https://leetcode.com/problems/implement-queue-using-stacks/ 题目:Implement the following operatio ...
- 【一天一道LeetCode】#232. Implement Queue using Stacks
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Impleme ...
随机推荐
- angular ng-repeat 动态获取的dom片段 显示
.filter('to_trusted', ['$sce',function ($sce) { return function (text) { return $sce.trustAsHtml(tex ...
- javascript 里面 with 关键字
1.with的基本概念 with语句的作用是将代码的作用域设置到一个特定的作用域中,目的是为了简化多次编写访问同一对象的工作.基本语法如下: with (expression) statement 下 ...
- Salesforce 外部对象
外部对象(External Object) 在Salesforce中,管理员或开发者可以通过"外部对象"将其他系统中的数据虚拟地展现为Salesforce的对象.每个外部对象都要连 ...
- T研究:国内云BPM市场规模尚小,预计2018年仅为3.29亿元
文章摘要:T研究发现,目前国内云BPM市场规模不高,预计今年为3.29亿元,不过其增速稳定,未来发展仍可期. BPM?什么鬼?反正作为“菊外人”的小编是第一次听说. 其实,对于这个词,不光是小编,国内 ...
- C# 实现FTP客户端
本文是利用C# 实现FTP客户端的小例子,主要实现上传,下载,删除等功能,以供学习分享使用. 思路: 通过读取FTP站点的目录信息,列出对应的文件及文件夹. 双击目录,则显示子目录,如果是文件,则点击 ...
- matlab练习程序(旋转矩阵、欧拉角、四元数互转)
欧拉角转旋转矩阵公式: 旋转矩阵转欧拉角公式: 旋转矩阵转四元数公式,其中1+r11+r22+r33>0: 四元数转旋转矩阵公式,q0^2+q1^2+q2^2+q3^2=1: 欧拉角转四元数公式 ...
- 《node.js权威指南》读书笔记
第一章 node.js介绍 非阻塞型I/O机制 当在访问数据库取得搜索结果的时候,在开始访问数据库之后,数据库返回结果之前,存在一段等待时间. 在传统的单线程处理机制中,在执行了访问数据库的代码之后, ...
- Win10更新
Turn: https://m.uczzd.cn/webview/news?app=meizubrw-iflow&aid=11529477703533248224&cid=100&am ...
- ssh-keygen的学习总结
ssh-keygen介绍 维基百科上关于ssh-keygen的介绍如下: ssh-keygen is a standard component of the Secure Shell (S ...
- ERROR 1050 (42S01): Table xxx already exists
今天遇到一个关于MySQL求助的问题,修改表结构时遇到"ERROR 1050 (42S01): table xxx already exits" mysql> ALTER ...