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. Postman前端HTTP请求调试神器教程

    Postman功能: 主要用于模拟网络请求包 快速创建请求 回放.管理请求 快速设置网络代理 我们看下界面: 一 接口请求流程: 二 postman使用   从流程图中我们可以看出,一个接口请求需要设 ...

  2. 网盘+SVN

    1.安装网盘 选择一个国内有名的网盘存储,例如金山网盘.360云盘等,注册账户会默认赠送几G的使用空间,然后下载其对应的网盘客户端管理软件(也可以使用浏览器方式),使用账号登录,就可以上传.管理文件等 ...

  3. 计蒜客 28202. Failing Components-最短路(Dijkstra) (BAPC 2014 Preliminary ACM-ICPC Asia Training League 暑假第一阶段第一场 B)

    B. Failing Components 传送门 题意就是单向图,从起点开始找最短路,然后统计一下个数就可以.方向是从b到a,权值为s. 直接最短路跑迪杰斯特拉,一开始用数组版的没过,换了一个队列版 ...

  4. 【hdu1150】【Machine Schedule】二分图最小点覆盖+简单感性证明

    (上不了p站我要死了,侵权度娘背锅) 题目大意 有两台机器A和B以及N个需要运行的任务.每台机器有M种不同的模式,而每个任务都恰好在一台机器上运行.如果它在机器A上运行,则机器A需要设置为模式ai,如 ...

  5. Hibernate使用Criteria去重distinct+分页

    写在前面: 最近在项目中使用了Criteria的分页查询,当查询的数据没有重复的记录还好,但是当数据有关联并出现重复记录的时候,就要去重,那么就会出现查询的记录数与实际的不一致的问题.这里也记录一下解 ...

  6. java多线程设计模式(3)读写锁模式

    1 Read-Write Lock Pattern Read-Write Lock Pattern是一种将对于共享资源的访问与修改操作分离,称为读写分离.即访问是reader,修改是write,用单独 ...

  7. java读取配置文件(properties)的时候,unicode码转utf-8

    有时我们在读取properties结尾的配置文件的时候,如果配置文件中有中文,那么我们读取到的是unicode码的中文,需要我们在转换一下,代码如下 /** * 将配置文件中的Unicode 转 ut ...

  8. 【Linux】CentOS7 alien命令 转化deb 与 rpm的相互转化

    -d, --to-deb Make debian packages. This is the default. -r, --to-rpm Make rpm packages. -t, --to-tgz ...

  9. 超级惊艳 10款HTML5动画特效推荐[转]

    ylbtech_html5_demo 今天我们要来推荐 10 款超级惊艳的 HTML5 动画特效,有一些是基于 CSS3 和 jQuery 的,比较实用,特别是前几个 HTML5 动画,简直酷毙了,现 ...

  10. Shell--变量的显示与设置、环境变量、语系变量

    1.变量的显示与设置:echo,unsetecho:显示一段文字,也可以读出变量内容并打印出来  格式echo $变量或者echo ${变量}语 法:echo [-neE][字符串]或 echo [- ...