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.

下面是错误答案。有待以后调试,不舍得放弃,留了下来:

数组模拟栈。入栈时直接统计最小值并放入数组

class MinStack {
public:
MinStack()
{
arr.resize(100000);//多么痛的领悟
minarr.resize(100000);
ntop=-1;
} void push(int x) {
++ntop;
arr[ntop]=x;
if(ntop==0)
minum=INT_MAX;
if(x<=minum)
minum=x;
minarr[ntop]=minum;
} void pop() {
minarr[ntop]=0;
ntop--;
} int top() {
return arr[ntop];
} int getMin() {
return minarr[ntop];
}
private:
vector<int> arr;
vector<int> minarr;
int ntop;
int minum;
};

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">

调试分析代码后已AC:

数组模拟栈

在压栈的时候,直接统计出当前最小值minum放入数组

出栈时,更新当前最小值minum(第一次忘了)~

class MinStack {
public:
MinStack()
{
arr.resize(100000);//多么痛的领悟
minarr.resize(100000);
ntop=-1;
} void push(int x) {
arr[++ntop]=x;
if(ntop==0)
minum=INT_MAX;
if(x<=minum)
minum=x;
minarr[ntop]=minum;
} void pop() {
minarr[ntop]=0;
ntop--;
minum=minarr[ntop];//上面的代码缺少这一行
} int top() {
return arr[ntop];
} int getMin() {
return minarr[ntop];
}
private:
vector<int> arr;
vector<int> minarr;
int ntop;
int minum;
};

学习别人家的算法设计:

他这样处理事实上还是在压栈时就获取了最小值。

相较普通的栈。题目要求多实现一个操作getMin(): 获取栈中最小的元素

我们维护两个栈:一个栈是普通栈s保存全部元素, 还有一个栈是最小栈mins保存s中的“曾出现过”的最小元素的递减序列。mins.top()即为getMin()的返回值。标识普通栈s里的最小元素。

考虑压栈 3 4 5 2 3 1, 它们有例如以下表现:

push   3 4 5 2 3 1

s      3 4 5 2 3 1

mins   3    2   1

亦即,当push(x)的x < mins.top()时,我们将x压入mins中。

大家能够发现。在上述push操作的随意间隔加我们若调用getMin()函数,mins.top()即为所求。

接下来考虑pop()操作,当且仅当s.top() == mins.top()时,我们才弹出mins的元素。这样就能够维护mins.top()始终为当前s里的最小值的性质。

class MinStack
{
public:
void push(int x)
{
s.push(x);
if (mins.empty() || x <= mins.top() )
mins.push(x);
} void pop()
{
if (mins.top() == s.top())
{
s.pop();
mins.pop();
} else {
s.pop();
}
} int top()
{
return s.top();
} int getMin()
{
return mins.top();
} private:
stack<int> s;
stack<int> mins;
};

注:本博文为EbowTang原创,兴许可能继续更新本文。假设转载,请务必复制本条信息!

原文地址:http://blog.csdn.net/ebowtang/article/details/50489486

原作者博客:http://blog.csdn.net/ebowtang

參考资源:

【1】网友。stephen_wong,博文地址。http://blog.csdn.net/stephen_wong/article/details/43924519

&lt;LeetCode OJ&gt; 155. Min Stack的更多相关文章

  1. 【leetcode❤python】 155. Min Stack

    #-*- coding: UTF-8 -*- class MinStack(object):    def __init__(self):        """      ...

  2. 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() { } v ...

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

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

  4. Java [Leetcode 155]Min Stack

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

  5. 155. Min Stack

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

  6. [LeetCode] 155. Min Stack 最小栈

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

  7. 【LeetCode】155. Min Stack 最小栈 (Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 栈同时保存当前值和最小值 辅助栈 同步栈 不同步栈 日期 题目地 ...

  8. Java for LeetCode 155 Min Stack

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

  9. 【leetcode】155 - Min Stack

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

随机推荐

  1. Selenium2+python自动化4-Pycharm使用【转载】

    前言 在写脚本之前,先要找个顺手的写脚本工具.python是一门解释性编程语言,所以一般把写python的工具叫解释器.写python脚本的工具很多,小编这里就不一一列举的,只要自己用着顺手就可以的, ...

  2. ubuntu 更换系统源和pip源

    1 . 备份 cd /etc/apt sudo cp sources-list sources-list.bak 2 . 编辑 这里用了阿里云的源 sudo vi sources-list 将文件内容 ...

  3. Linux下多进程服务端客户端模型一(单进程与多进程模型)

    本文将会简单介绍Linux下如何利用C库函数与系统调用编写一个完整的.初级可用的C-S模型. 一.基本模型: 1.1   首先服务器调用socket()函数建立一个套接字,然后bind()端口,开始l ...

  4. TreeSet与TreeMap排序

    1.TreeSet原理: /* * TreeSet存储对象的时候, 可以排序, 但是需要指定排序的算法 *  * Integer能排序(有默认顺序), String能排序(有默认顺序), 自定义的类存 ...

  5. Python_Tips[3] -> sort/sorted 排序函数

    排序函数 / Sort Function list自带的sort函数可以实现对列表的排列功能,具有同样功能的还有sorted函数. 基本形式 列表有自己的sort方法,其对列表进行原址排序,既然是原址 ...

  6. hiho一下第131周 后缀自动机二·重复旋律8(循环相似子串)

    后缀自动机五·重复旋律8 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi平时的一大兴趣爱好就是演奏钢琴.我们知道一段音乐旋律可以被表示为一段数构成的数列. 小Hi ...

  7. [HDU6223]Infinite Fraction Path

    题目大意: 有$n(n\leq 150,000)$个编号为$0_n-1$格子,每个格子有一个权值$w_i(0\leq w_i\leq 9)$.从任意一个点出发,按照一定的规则进行跳转.设当前的格子为$ ...

  8. C++中static、const使用方法简介

    众所周知,在c++中,static和const的使用方法是难点,也是重点,值得我们随时温习,所谓温故而知新是也. 下面我们首先说一说static. 1.static的作用 static变量的作用,主要 ...

  9. JavaEE目录

    第一章: Spring介绍 Spring项目搭建 Spring概念 第二章: Sprin配置详解 属性注入(构造方法注入,设值注入) 实例化(构造器(空参构造器),静态工厂,工厂方法) 装配(xml方 ...

  10. Word插入页码简单方法

    适用于只有一个首页,若是从某一页,需要分节符,从某一页参考Word2007插入两种页码. 插入-页码-页面低端-弹到设计窗口.然后选择页码-设置页码格式-起始页码0-勾选首页不同.