2016.6.1——Min Stack
Min Stack
本题收获:
1.可以利用两个栈操作。
2.栈的基本操作。
题目:
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.
- push(x) -- Push element x onto stack.
- pop() -- Removes the element on top of the stack.
- top() -- Get the top element.
- getMin() -- Retrieve the minimum element in the stack.
Example:
MinStack minStack = new MinStack();
minStack.push(-2);
minStack.push(0);
minStack.push(-3);
minStack.getMin(); --> Returns -3.
minStack.pop();
minStack.top(); --> Returns 0.
minStack.getMin(); --> Returns -2.
思路:
我的思路:没有思路。
leetcode/dicuss思路:
弄2个stack,一个realStack,存放真正的数据;另外一个是minStack。
对于minStack元素中的每一个元素的意义是:push到 该位置的时候,当前最小元素的值。每次push进新元素的时候,更新minStack的值;每次pop的时候,由于minStack的定义,所以只需把 minStack和realStack一起进行一次pop操作就好了。
minstack只存储最小元素,每次Push时将输入元素与minstack中的元素对比,如果小于minstack则push,否则不操作。pop时,如果minstack的元素等于realstack中元素则pop,否则不操作。
代码:
class MinStack {
private:
stack<int> s1;
stack<int> s2;
public:
void push(int x) {
s1.push(x);
if (s2.empty() || x <= getMin()) s2.push(x);
}
void pop() {
if (s1.top() == getMin()) s2.pop();
s1.pop();
}
int top() {
return s1.top();
}
int getMin() {
return s2.top();
}
};
我的带mian函数的测试代码:
// minStack.cpp : 定义控制台应用程序的入口点。
// #include "stdafx.h"
#include "iostream"
#include "stack"
using namespace std; class MyClass
{
public:
void push(int val);
void pop();
int minStack();
int top();
private:
stack<int> realstack;
stack<int> minstack;
}; void MyClass::push(int val)
{
realstack.push(val);
if (minstack.empty() || val <= minstack.top()) minstack.push(val);
} void MyClass::pop()
{
if (realstack.top() == minstack.top()) //注意pop的顺序,先比较在pop,不是先pop在比较
{
minstack.pop();
}
realstack.pop();
} int MyClass::top()
{
return realstack.top();
} int MyClass::minStack()
{
return minstack.top();
} int _tmain(int argc, _TCHAR* argv[])
{
MyClass solution;
int val = ;
cout << "the value is : ";
for (int i = ; i < ; i++)
{
cin >> val;
solution.push(val);
}
cout << "the min is : " << solution.minStack() << endl;
for (int i = ; i < ; i++)
{
int value = solution.top();
cout << "pop value is : " << value << endl;
//cin >> val;
solution.pop();
}
cout << "current min is : " << solution.minStack() << endl;
system("pause");
return ;
}
测试结果:
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAUgAAACoCAIAAAANReBSAAAQmUlEQVR4nO2d6VbbSLeGfSGHLwlDEqATIASMjcHzLNmy5EkewGZKp/vrPrd0ICEMCTPp/sulnR8yha2hVJJtDJX3WXuxTLG1q2TXo7JlGXukUt0Y+VJNF1K5TqJQacjVplxtFmub5Uar0mirG9u11k69vdfc/n1z5/Pm7uf23p9bn/678/mvnc9/7/7x996f/6vF7h8msfP5b9vY/v2v7d//2v70EFuGaO/9V4vW3p+t3T+6Y3PnM4mN7d+1aG59arT3Gu29emuv3tqrtfZqrT21tau2dtXNHXVzR93Yrja3Ko1OlBvtUr1VqnWiqLYUdVOpbijVDbnaVKpNudIoVJpypSlXNuTqpqKF2irWtkq1rVJ9u1TfKTV2ys3dSnO3TKKxU27slBvbWpQaW6XGVrG+Vay3FbUlVzcLlY18qS4oakYqp0QlkZXi6VwsLUZTQiSZDSey4UQ2lMiQCMbTwXgmmMiEEtlQIhtKCuGMFBaUSK4cLahRpRErbcar7bi6Ha9uxavtWKUdK7dixY2oUo8W1Ei+EhGUcLYQSuVCCSGUEEKJbDCeCcbTwVgK8SzCw2K10W2d3iXN8OaWurFd3diube7U27uN9l5z61Nz+9ODSGbR2PpEj3p7rxOtXRK13uh4+GDjQxAtK42tcr1NolRrFdVNRd3QQq5uyNWNQrVZqDS0kMr1fKmWK2qh5oqqqKiiXBXlqlCoZqVKJl/WIp0vpXLF+yilOz/L6Vw5k69kpGqmoAqyKsg1QamLxYZYbOaKzfsbjfuoi8WaWKwJSieyspqRqul8JZUrJ0QllpFCCTEYS69FkquhuD8U861HV9YiK4HISiDiDYS9gfDyaug+wsur4eVA2BuIeNei3mB8JZxaiWZ8cdGXzPvT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/xAaAEzSrITYAXEGsnv3gg9gAcIJmNcQGgCuI1RAbAH4gVkNsAPiBWD37wffl8BhiA8ADD2IvrEBsADiBWD27sHIAsQHgA4gNAIcQq2cgNgDcQKyG2ADwA7EaYgPAD0Ts6XkvxAaAE2YgNgD8QayG2ADwA7EaYgPADxAbAA4hVkNsAPgBYgPAIRAbAA6B2ABwCMQGgEMgNgAcArEB4BCIDQCHQGwAOARiA8AhEBsADukWe//rEcQGgAeI1W/nliE2AJxArIbYAPADsRpiA8APEBsADiFWQ2wA+AFiA8AhEBsADoHYAHAIxAaAQyA2ABwCsQHgEIgNAIdAbAA4BGIDwCEQGwAOgdgAcAjEBoBDIDYAHAKxAeAQiA0Ah0BsADgEYgPAIRAbAA6B2ABwCMQGgEMgNgD88f+Wg8I2DITraAAAAABJRU5ErkJggg==" alt="" />
参考了:http://www.cnblogs.com/lihaozy/archive/2012/12/09/2809840.html
写的非常详细!
代码2:
class MinStack {
public:
vector<int> a;
vector<int> min;
MinStack() {
min.push_back();
}
void push(int x) {
a.push_back(x);
if (x < min.back()) {
min.push_back(x);
} else {
min.push_back(min.back());
}
} void pop() {
a.pop_back();
min.pop_back();
} int top() {
return a.back();
} int getMin() {
return min.back();
}
};
2016.6.1——Min Stack的更多相关文章
- [LintCode] Min Stack 最小栈
Implement a stack with min() function, which will return the smallest number in the stack. It should ...
- [CareerCup] 3.2 Min Stack 最小栈
3.2 How would you design a stack which, in addition to push and pop, also has a function min which r ...
- leetcode 155. Min Stack --------- java
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...
- Min Stack [LeetCode 155]
1- 问题描述 Design a stack that supports push, pop, top, and retrieving the minimum element in constant ...
- Min Stack
Min Stack Design a stack that supports push, pop, top, and retrieving the minimum element in constan ...
- Java [Leetcode 155]Min Stack
题目描述: Design a stack that supports push, pop, top, and retrieving the minimum element in constant ti ...
- 155. Min Stack
题目: Design a stack that supports push, pop, top, and retrieving the minimum element in constant time ...
- leetCode Min Stack解决共享
原标题:https://oj.leetcode.com/problems/min-stack/ Design a stack that supports push, pop, top, and ret ...
- LeetCode算法题-Min Stack(Java实现)
这是悦乐书的第177次更新,第179篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第36题(顺位题号是155).设计一个支持push,pop,top和在恒定时间内检索最小 ...
随机推荐
- UVAlive4287_Proving Equivalences
题意是告诉你有n个命题,m条递推关系,表示某个命题可以推出另外一个命题. 现在问你至少在增加多少个递推关系可以保证所有命题两两互推. 命题为点,关系为有向边,题目转化成为至少增加多少条有向边使得整个图 ...
- HDU3829_Cat VS Dog
题目是这样的,给定一些人喜欢某只猫或者狗,讨厌某只猫或者狗.求最多能够同时满足多少人的愿望? 题目很有意思.建模后就很简单了. 对于同一只猫或者狗,如果有一个讨厌,另一个人喜欢,那么这两个连一条边.最 ...
- [洛谷P3829][SHOI2012]信用卡凸包
题目大意:有$n$张一模一样的信用卡,每个角进行了圆滑处理,问这些卡组成的“凸包”的周长 题解:发现是圆滑处理的圆心围成的凸包加上一个圆周即可 卡点:输入长宽弄反,然后以为是卡精 C++ Code: ...
- Vue里边接口访问Post、Get
原文地址: http://www.cnblogs.com/JimmyBright/p/7356502.html 通常js里面都用ajax来和服务器交换数据,Vue里边当然也可以用ajax,ajax是基 ...
- Spring点滴一:Spring Ioc 容器
Spring 容器: Spring 容器是Spring框架的核心.Spring容器将创建Bean对象实例,把它们联系在一起,配置它们,并管理它们整个生命周期从创建到销毁.Spring 容器通过依赖注入 ...
- BZOJ4727 [POI2017]Turysta 【竞赛图哈密顿路径/回路】
题目链接 BZOJ4727 题解 前置芝士 1.竞赛图存在哈密顿路径 2.竞赛图存在哈密顿回路,当且仅当它是强联通的 所以我们将图缩点后,拓扑排序后一定是一条链,且之前的块内的点和之后块内的点的边一定 ...
- 【codeforces 765F】 Souvenirs
http://codeforces.com/problemset/problem/765/F (题目链接) 题意 给出$n$个数的序列,$m$次询问,每次查询区间$[l,r]$之间相差最小的两个数的差 ...
- md5sum命令行使用注意事项
Linux下md5sum命令可以直接计算标准输入的hash值,使用方式为 echo -n '123456' | md5sum 注意给echo加上-n表示在尾部不加换行 命令行示例 $ echo -n ...
- Python入门 语法
Python入门 语法 语言介绍 对象,类型,值 编码规范 https://www.python.org/dev/peps/pep-0008/ 一.语言介绍 编程语言: 机器语言,汇编语言,高级语言 ...
- Solr常用命令总结
前提条件: 安装solr版本:4.8.0 部署solr路径:/data/solr-4.8.0 1. 通过zookeeper上传一些配置信息: 通过zk命令将配置信息上传到zk环境中: /data/so ...