<LeetCode OJ> 155. Min Stack
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
<LeetCode OJ> 155. Min Stack的更多相关文章
- 【leetcode❤python】 155. Min Stack
#-*- coding: UTF-8 -*- class MinStack(object): def __init__(self): """ ...
- 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 ...
- leetcode 155. Min Stack --------- java
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...
- 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] 155. Min Stack 最小栈
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...
- 【LeetCode】155. Min Stack 最小栈 (Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 栈同时保存当前值和最小值 辅助栈 同步栈 不同步栈 日期 题目地 ...
- Java for LeetCode 155 Min Stack
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...
- 【leetcode】155 - Min Stack
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...
随机推荐
- linux nbd & qemu-nbd
网络块设备: Network Block Device 可以将一个远程主机的磁盘空间,当作一个块设备来使用.就像一块硬盘一样. 使用它,你可以很方便的将另一台服务器的硬盘空间,增加到本地服务器上 ...
- java copy array
13down voteaccepted Here's a java 1.4 compatible 1.5-liner: int[] array = { 1, 2, 3, 4, 5 }; int siz ...
- 基于Bootstrap的表格插件bootstrap-table
写在前面: 表格在项目中是使用比较多的,bootstrap-table插件也是非常好用,而且表格页面也比较好看.这里也简单的记录下. 下面直接看demo吧,代码中都注释了,有些用法,这里没有用到,需要 ...
- ffmpeg yasm not found, use --disable-yasm for a crippled build
yasm是汇编编译器,因为ffmpeg中为了提高效率用到了汇编指令,比如MMX和SSE.解决这个问题方面有两个: 1.在网上下载一个yasm.exe并安装在mingw/bin下面,编译代码时你注意看, ...
- TDD的iOS开发初步以及Kiwi使用入门
测试驱动开发(Test Driven Development,以下简称TDD)是保证代码质量的不二法则,也是先进程序开发的共识.Apple一直致力于在iOS开发中集成更加方便和可用的测试,在Xcode ...
- selenium 截图
http://blog.csdn.net/u010953692/article/details/78320025 # coding:utf-8 # coding:cp936 from selenium ...
- ubuntu 16.04 网络配置之虚拟网卡的配置
关于图形界面的配置,我这里就不多介绍了,这个很简单.这里介绍的是如何通过修改配置文件来实现虚拟网卡. 首先介绍ubuntu(我这里使用的是ubuntu-16.04)下虚拟网卡的配置 1.先用ifcon ...
- 面试题:如何在不使用临时变量temp的情况下交换两个整数的值?
利用一个小技巧,一个整数a在异或另一个整数b两次以后所得的值还是整数a. 具体的过程我们可以自己找两个整数以二进制的形式自己在纸上画一下他们的异或过程.(异或的运算符号为"^") ...
- 2017.6.27 jdbc基本使用
参考来自:http://www.runoob.com/w3cnote/jdbc-use-guide.html 1.jdbc的执行流程 JDBC API 允许用户访问任何形式的表格数据,尤其是存储在关系 ...
- Transform.Translate 平移
function Translate (translation : Vector3, relativeTo : Space = Space.Self) : void Description描述 Mov ...