https://leetcode.com/problems/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.

思路:

关键在于找最小值要O(1)复杂度,所以空间换时间。一个额外的vector存储递减序入栈的数字。

AC代码:

 class MinStack {
public:
MinStack(){ st_min.push_back(INT_MAX); };
void push(int x) {
if (x <= st_min[st_min.size()-])
st_min.push_back(x);
st.push_back(x);
return;
} void pop() {
if (st[st.size() - ] == st_min[st_min.size() - ]){
st_min.pop_back();
}
st.pop_back();
return;
} int top() {
return st[st.size() - ];
} int getMin() {
return st_min[st_min.size() - ];
} private:
vector<int> st;
vector<int> st_min;
};

LeetCode(155)题解--Min Stack的更多相关文章

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

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

  2. LeetCode(155) Min Stack

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

  3. LeetCode OJ:Min Stack(最小栈问题)

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

  4. LeetCode 155:最小栈 Min Stack

    LeetCode 155:最小栈 Min Stack 设计一个支持 push,pop,top 操作,并能在常数时间内检索到最小元素的栈. push(x) -- 将元素 x 推入栈中. pop() -- ...

  5. Min Stack [LeetCode 155]

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

  6. 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 ...

  7. leetcode 155. Min Stack --------- java

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

  8. Java [Leetcode 155]Min Stack

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

  9. 155. Min Stack

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

随机推荐

  1. django无法同步数据库 Error loading MySQLdb module: No module named ‘MySQLdb‘

    最近在学习Python,打算先看两个在线教程,再在github上找几个开源的项目练习一下,在学到“被解放的姜戈”时遇到django同步数据库时无法执行的错误,记录一下. 错误现象: 执行python ...

  2. SharePoint 2013 Custom MasterPage

    <%@Master language="C#"%> <%@ Register Tagprefix="SharePoint" Namespace ...

  3. How to build and run ARM Linux on QEMU from scratch

    This blog shows how to run ARM Linux on QEMU! This can be used as a base for later projects using th ...

  4. brk(), sbrk() 用法详解【转】

    转自:http://blog.csdn.net/sgbfblog/article/details/7772153 贴上原文地址,好不容易找到了:brk(), sbrk() -- 改变数据段长度 brk ...

  5. 阿里云oss教程

    OSS是提供非结构化数据存取的服务.对于刚开始使用OSS的用户,非结构数据可以理解为word文档.PDF.PPT.EXCEL表格.MP3.MKV.RMVB.HTML等各种类型文件.OSS提供API去进 ...

  6. POJ - 2135最小费用流

    题目链接:http://poj.org/problem?id=2135 今天学习最小费用流.模板手敲了一遍. 产生了一个新的问题:对于一条无向边,这样修改了正向边容量后,反向边不用管吗? 后来想了想, ...

  7. R语言集合函数

    union intersect setdiff(A,B):A-B A %in% B :A是否存在于B

  8. 2017 CCPC 哈尔滨站 题解

    题目链接  2017 CCPC Harbin Problem A Problem B Problem D Problem F Problem L 考虑二分答案. 设当前待验证的答案为x 我们可以把第二 ...

  9. tomcat7.0.55配置单向和双向HTTPS连接

    HTTPS配置中分为单向连接和双向连接,单向连接只需要服务器安装证书,客户端不需要,双向连接需要服务器和客户端都安装证书 下面的配置都没有用CA签名来配置,都不能用于生产环境,实际配置中是需要CA的, ...

  10. Go语言入门——数组、切片和映射(下)

    上篇主要介绍了Go语言里面常见的复合数据类型的声明和初始化. 这篇主要针对数组.切片和映射这些复合数据类型从其他几个方面介绍比较下. 1.遍历 不管是数组.切片还是映射结构,都是一种集合类型,要从这些 ...