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和在恒定时间内检索最小 ...
随机推荐
- JVM内存管理机制
Java与C++之间有一堆由内存动态分配与垃圾收集技术所围成的“高墙”,墙外面的人想进去,墙里面的人却想出来. —— <深入理解Java虚拟机:JVM高级特性与最佳实践> Java虚拟机在 ...
- DAY...
讲道理,我还是以前的我.没有坚持每天写......... 又到了谷底......... 坚持啊........
- url基础知识
浏览器通过url访问服务器步骤 ①浏览器解析出url中的服务器名称 ②浏览器将服务器名称解析成ip(DNS解析) ③浏览器解析出url中的服务器端口(如果有端口的话) ④浏览器建立和web服务器的TC ...
- git gitignore 如何添加,为何添加了无效
需求:一个新项目源码要挂载在GIT服务器上,但是里面的obj文件夹,bin文件夹,.exe文件不提交(每次) 有两种情况出现 1.项目初始化的时候就加入拦截规则文件 gitignore 具体步骤请参 ...
- MyBatis.1入门篇
一:简介 MyBatis是一个优秀的持久层框架,它对jdbc的操作数据库的过程进行封装,使开发者只需要关注 SQL 本身,而不需要花费精力去处理例如注册驱动.创建connection.创建statem ...
- Java EE之Servlet
1.创建Servlet类 Servlet在Java EE API规范中的定义: Servlet是一个运行在Web服务器中的Java小程序.Servlet将会接收和响应来自Web客户端的请求,使用HTT ...
- fzyjojP2931 乱搞
其实很简单(第二个不知是啥) 贡献独立 其实第一种就是考虑一个点在哈夫曼树上的期望深度是多少 因为精度要求较高 所以要高精小数加,高精小数除以低精整数
- easyUI datetimebox 自定义显示格式
http://blog.csdn.net/littlewolf766/article/details/7329123 项目里需要显示自定义显示格式,比如只显示yyy,yyyy-mm,yyyy-mm-d ...
- 并发库应用之一 & ThreadLocal实现线程范围的共享变量
ThreadLocal用于实现线程内的数据共享,即对于相同的程序代码,多个模块在同一个线程中运行时要共享一份数据,而在另外线程中运行时又共享另外一份数据. 每个线程调用全局ThreadLocal对象的 ...
- JavaScript中的apply()和call()
可以将call()和apply()看做是某个对象的方法,通过调用方法的形式来间接调用函数. call()和apply()的第一个实参是要调用函数的母对象,它是调用上下文,在函数体内通过this来获得对 ...