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 ...
随机推荐
- Android EditText手机号格式化输入XXX-XXXX-XXXX
先来效果图: 设置手机格式化操作只需要设置EditText的addTextChangedListener的监听,下面看代码 /*editText输入监听*/ et_activity_up_login_ ...
- react-fetch数据发送请求
在一个项目中,数据的请求发送数据是最为重要的,不可能我们的数据都是自己进行编写的 在react中官方推荐使用的方法是fetch.当然它里面也可以使用vue中的axios请求数据,jQuery的$.aj ...
- IPD咨询如何才能真正落地?
文/资深顾问 杨学明 IPD作为先进的产品开发理念,思想起源于PRTM公司,PACE,培思的力量,首先在IBM和波音公司迅速完善,中国是深圳华为公司. 1992年IBM公司利润停止增长,财务困难,IB ...
- Keras深度学习框架安装及快速入门
1.下载安装Keras 如果你是安装的Anaconda组合套件,可以直接在Prompt上执行安装命令:pip install keras 注意:最下面为Successfully...表示安装成功! 2 ...
- c/c++ const关键字
const 在星号的右边:不可以改指针的指向,可以用指针改里面的值 int * const p; const在星号的左边:可以改指针的指向,不可以用指针改里面的值 int const *p; cons ...
- Lua保留指定小数位数
默认会四舍五入 比如:%0.2f 会四舍五入后,保留小数点后2位 print(string.format("%.1f",0.26)) ---会输出0.3,而不是0.2 Lua保留一 ...
- vmware中连接U盘
1.首先安装VMware tools. 2.然后根据这篇经验操作 https://jingyan.baidu.com/article/7c6fb42828806480652c9062.html 3. ...
- Tomcat优化配置
1.环境: 系统:Windows.Linux Tomcat版本:9 2.编码与性能调优 server.xml文件: <Connector port="8080" connec ...
- 设置SSH免密码登录
1.cd .ssh 2.执行下面的命令,三次回车. ssh-keygen -t rsa cat id_rsa.pub >> authorized_keys 3.发送公钥 scp .ssh/ ...
- Git Extensions 使用小结
1.查看仓库 2.创建分支 然后会自动创建一个 Commit ,推送到远端分支即可. 3.合并分支 注意1.自动提交需要没有无法自动合并的冲突才行. 注意2.快进线指的是将别人的提交原封不动附加到自己 ...