博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
leetcode 155 Min Stack
阅读量:6189 次
发布时间:2019-06-21

本文共 2772 字,大约阅读时间需要 9 分钟。

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.

解决方案:

这里写图片描述

可以看到STL的解决方案跟大多数的c语言解决方案还是有差距的,后序我找一找基于链表的整齐点的c语言实现

The key idea is use a another stack to store the minimum value of the corresponding stack. Put differently, min[i] equals the minimum element where data[i] is the top of this sub-stack.

We can use a full size of min where it’s size equals the data’s, but it’s not necessary.

I have 2 main concerns about the algorithm:

1

We should pop the element in min IFF there’s match of data.top().

2

If we have multiple minima, for example [0, 1, 0] in data, then the min should be [0, 0].
Otherwise, the the pop operation wouldn’t work properly.
As a result, we should push the element if x <= min.top().

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

STL list实现:

class MinStack {    private:        list
s; int min; public: MinStack() { min=INT_MAX; } void push(int x) { if(x
::iterator it=s.begin(); while(it!=s.end()) { if(*it

python解决方案:

class MinStack:# @param x, an integerdef __init__(self):    # the stack it self    self.A = []    self.minS=[]# @return an integerdef push(self, x):    n=len(self.A)    if n==0:        self.minS.append(x)    else:        lastmin=self.minS[-1]        if x<=lastmin:            self.minS.append(x)    self.A.append(x)# @return nothingdef pop(self):    if len(self.A)>0 and self.A.pop()==self.minS[-1]:        self.minS.pop()# @return an integerdef top(self):    return self.A[-1]# @return an integerdef getMin(self):    return self.minS[-1]

python解决方案2:

class MinStack:def __init__(self):    self.q = []# @param x, an integer# @return an integerdef push(self, x):    curMin = self.getMin()    if curMin == None or x < curMin:        curMin = x    self.q.append((x, curMin));# @return nothingdef pop(self):    self.q.pop()# @return an integerdef top(self):    if len(self.q) == 0:        return None    else:        return self.q[len(self.q) - 1][0]# @return an integerdef getMin(self):    if len(self.q) == 0:        return None    else:        return self.q[len(self.q) - 1][1]  asked Apr 14  in Min Stack  by  charles8135 (180 points)
你可能感兴趣的文章
Linux安装搭建第三篇(redis安装与集群)
查看>>
Shiro详细配置-基于Spring3.2 shiro2.2
查看>>
resizableImageWithCapInsets图片拉伸
查看>>
多用户登录自动保存history到统一目录
查看>>
Spring 多线程下注入bean问题详解
查看>>
使用apache.tika判断文件类型
查看>>
正则表达式速成
查看>>
linux 特殊符号的处理
查看>>
oracle 体系结构及内存管理 03_oracle官方文档索引
查看>>
《Spring Recipes》第三章笔记4:Reusing Pointcut Definit...
查看>>
VS2017 C# 中 搭建 CEF3 WinForm 开发环境
查看>>
免费在线PDF转换成Word转换器独家测试
查看>>
mysql 服务丢失
查看>>
Trace ODBC Connection log
查看>>
从0开始写JavaWeb框架系列(8)从0开始写SamrtFrameWork:项目使用的工具类一览
查看>>
mysql 分表分区小记(二)
查看>>
IE8 中"HTML Parsing Error:Unable to modify the p...
查看>>
【虚拟机】批处理文件启动virtualBox小记
查看>>
Angular学习
查看>>
Solr/Lucene escape char handling
查看>>