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+wsur4eVA2BuIeNei3mB8JZx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ffVyF2ABwArH6t0U/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的更多相关文章

  1. [LintCode] Min Stack 最小栈

    Implement a stack with min() function, which will return the smallest number in the stack. It should ...

  2. [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 ...

  3. leetcode 155. Min Stack --------- java

    Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...

  4. Min Stack [LeetCode 155]

    1- 问题描述 Design a stack that supports push, pop, top, and retrieving the minimum element in constant ...

  5. Min Stack

    Min Stack Design a stack that supports push, pop, top, and retrieving the minimum element in constan ...

  6. Java [Leetcode 155]Min Stack

    题目描述: Design a stack that supports push, pop, top, and retrieving the minimum element in constant ti ...

  7. 155. Min Stack

    题目: Design a stack that supports push, pop, top, and retrieving the minimum element in constant time ...

  8. leetCode Min Stack解决共享

    原标题:https://oj.leetcode.com/problems/min-stack/ Design a stack that supports push, pop, top, and ret ...

  9. LeetCode算法题-Min Stack(Java实现)

    这是悦乐书的第177次更新,第179篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第36题(顺位题号是155).设计一个支持push,pop,top和在恒定时间内检索最小 ...

随机推荐

  1. Java容器深入浅出之HashSet、TreeSet和EnumSet

    Java集合中的Set接口,定义的是一类无顺序的.不可重复的对象集合.如果尝试添加相同的元素,add()方法会返回false,同时添加失败.Set接口包括3个主要的实现类:HashSet.TreeSe ...

  2. linux下安装java jdk

    第一步:查看java对应版本               yum search java 我自己装的是1.8版本的java包 第二步:装java包 yum install java-1.8.0-ope ...

  3. [洛谷P4723]【模板】线性递推

    题目大意:求一个满足$k$阶齐次线性递推数列$a_i$的第$n$项. 即:$a_n=\sum\limits_{i=1}^{k}f_i \times a_{n-i}$ 题解:线性齐次递推,先见洛谷题解, ...

  4. Linux下的bash对拍

    下面是Linux下的bash对拍程序: #!/bin/bash t=0 //数据组数 while true; do let "t=$t + 1" echox printf $t / ...

  5. 破解PostgresSQL登录的6种方法

      第一种方式Hydra: Hydra通常是首选工具,它可以对50多种协议执行快速字典暴力攻击,包括telnet,postgres,http,https,smb服务和各种数据库等.现在需要选择一个字典 ...

  6. Android中的Surface, SurfaceHolder, SurfaceHolder.Callback, SurfaceView

    传入一个surface,然后让openGL在surface上画图 window->view hierachy(DecorView是tree的root)->ViewRoot->Surf ...

  7. python print end=' ' 不换行

    python3.x 实现print 不换行 python中print之后是默认换行的,是因为其默认属性 end 默认值为"\n"(\n为换行符). 做练习99乘法表时不想换行,改变 ...

  8. 设计模式之单例模式实现(C++)

    #ifndef SINGLETON_H #define SINGLETON_H #include <cassert> #include <memory> #include &l ...

  9. lightoj 1205 数位dp

    1205 - Palindromic Numbers    PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 3 ...

  10. Java入门:基础算法之获取用户输入

    本部分演示如何获取用户输入.我们使用Scanner类来得到用户输入.下面的实例代码中演示了如何获取用户输入的字符串.整数和float数据.主要用到了以下方法: 1)public String next ...