设计一个支持 push,pop,top 操作,并能在常量时间内检索最小元素的栈。
    push(x) -- 将元素x推入栈中。
    pop() -- 删除栈顶的元素。
    top() -- 获取栈顶元素。
    getMin() -- 检索栈中的最小元素。
示例:
MinStack minStack = new MinStack();
minStack.push(-2);
minStack.push(0);
minStack.push(-3);
minStack.getMin();   --> 返回 -3.
minStack.pop();
minStack.top();      --> 返回 0.
minStack.getMin();   --> 返回 -2.
详见:https://leetcode.com/problems/min-stack/description/

Java实现:

方法一:

class MinStack {
private Stack<Integer> stk;
private Stack<Integer> minStk; /** initialize your data structure here. */
public MinStack() {
stk=new Stack<Integer>();
minStk=new Stack<Integer>();
} public void push(int x) {
stk.push(x);
if(minStk.isEmpty()){
minStk.push(x);
}else if(minStk.peek()>=x){
minStk.push(x);
}
} public void pop() {
int top=stk.pop();
if(minStk.peek()==top){
minStk.pop();
}
} public int top() {
return stk.peek();
} public int getMin() {
return minStk.peek();
}
} /**
* Your MinStack object will be instantiated and called as such:
* MinStack obj = new MinStack();
* obj.push(x);
* obj.pop();
* int param_3 = obj.top();
* int param_4 = obj.getMin();
*/

方法二:

class MinStack {
private Stack<Integer> stk;
private Stack<Integer> minStk; /** initialize your data structure here. */
public MinStack() {
stk=new Stack<Integer>();
minStk=new Stack<Integer>();
} public void push(int x) {
stk.push(x);
if(minStk.isEmpty()){
minStk.push(x);
}else if(minStk.peek()>=x){
minStk.push(x);
}else{
minStk.push(minStk.peek());
}
} public void pop() {
stk.pop();
minStk.pop();
} public int top() {
return stk.peek();
} public int getMin() {
return minStk.peek();
}
} /**
* Your MinStack object will be instantiated and called as such:
* MinStack obj = new MinStack();
* obj.push(x);
* obj.pop();
* int param_3 = obj.top();
* int param_4 = obj.getMin();
*/

155 Min Stack 最小栈的更多相关文章

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

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

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

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

  3. [CareerCup] 3.2 Min Stack 最小栈

    3.2 How would you design a stack which, in addition to push and pop, also has a function min which r ...

  4. [LeetCode] Min Stack 最小栈

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

  5. lintcode 中等题:Min stack 最小栈

    题目 带最小值操作的栈 实现一个带有取最小值min方法的栈,min方法将返回当前栈中的最小值. 你实现的栈将支持push,pop 和 min 操作,所有操作要求都在O(1)时间内完成. 解题 可以定义 ...

  6. [LeetCode] 0155. Min Stack 最小栈 & C++Runtime加速

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

  7. [LintCode] Min Stack 最小栈

    Implement a stack with min() function, which will return the smallest number in the stack. It should ...

  8. 第30题:LeetCode155. Min Stack最小栈

    设计一个支持 push,pop,top 操作,并能在O(1)时间内检索到最小元素的栈. push(x) -- 将元素 x 推入栈中. pop() -- 删除栈顶的元素. top() -- 获取栈顶元素 ...

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

随机推荐

  1. 火焰灯menu修改之后,可以实现数遍点击小方块停留在当前页面

    下载地址:http://www.cnblogs.com/RightDear/admin/Files.aspx 调用方式,传入一个参数 首页传入0,关于联盟传入1,产品展示传入2,依此类推 <sc ...

  2. Django 之ORM操作

    1.什么是ORM? 全称关系对象映射Object Relational Mapping(简称ORM),是通过描述面向对象与数据库之间的对应的元数据,将对象持久化的更新到数据库中. 有了ORM,就不需要 ...

  3. 线程安全 对StringBuilder抛出ArrayIndexOutOfBoundsException的探究

    对StringBuilder抛出ArrayIndexOutOfBoundsException的探究 - CSDN博客 https://blog.csdn.net/liu_005/article/det ...

  4. HDU2089 不要62 —— 数位DP

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2089 不要62 Time Limit: 1000/1000 MS (Java/Others)    M ...

  5. js split分割字符串成数组

    str = "2,2,3,5,6"; //这是一字符串 var strs = new Array(); //定义一数组 strs = str.split("," ...

  6. Couldn't connect to host, port: smtp.163.com, 25; timeout -1;

    运行出现以下报错: Couldn't connect to host, port: smtp.163.com, 25; timeout -1; 也要设置端口 spring.mail.port=25

  7. SPOJ:One piece(不错的带权括号最大匹配问题)

    One of DB and TN common interests is traveling. One day, they went to Grand Line and found One Piece ...

  8. 【字符串】BZOJ上面几个AC自动机求最为字串出现次数的题目

    (一下只供自己复习用,目的是对比这几个题,所以写得不详细.需要细节的可以参考其他博主) [BZOJ3172:单词] 题目: 某人读论文,一篇论文是由许多(N)单词组成.但他发现一个单词会在论文中出现很 ...

  9. servlet中的servletURL,servletURI和servletPath

    String    servletURL=request.getservletURL(); url:站点名+当前web应用名+(目录名)+页面名 String    servletURI=reques ...

  10. http基础知识摘录

    HTTP是一个基于请求/响应模式的,无状态的协议 (只有客户端发送请求服务器才会响应,否则服务器不会主动发送信息的,无状态指客户端发过来一个请求服务端给你发回一个响应,接着你再去发送一个请求,服务器根 ...