关闭

leetcode(155):Min Stack

标签: leetcode设计
344人阅读 评论(0) 收藏 举报
分类:

**【题目】
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.

Example:

MinStack minStack = new MinStack();
minStack.push(-2);
minStack.push(0);
minStack.push(-3);
minStack.getMin(); –> Returns -3.
minStack.pop();
minStack.top(); –> Returns 0.
minStack.getMin(); –> Returns -2.**

【分析】
题意为设计一个栈,且是”最小”栈,其中的getMin方法返回栈中的最小值。
思路是声明一个主栈和一个辅助栈,这个栈的栈顶保存的是入主栈的元素的最小值,这样getMin方法返回的始终是当前所有元素的最小值,时间复杂度为O(1).

Java实现:

public class MinStack {

    Stack<Integer> s,minS;

    /** initialize your data structure here. */
    public MinStack() {
       s= new Stack<>();
       minS = new Stack<>();
    }

    public void push(int x) {
        s.push(x);
        if(!minS.isEmpty() && minS.peek()<x) return ;
        minS.push(x);
    }

    public void pop() {
        int m = s.pop();
        if(m==minS.peek()) minS.pop();
    }

    public int top() {
        return s.peek();

    }

    public int getMin() {
        return minS.peek();

    }

}
0
0
查看评论

LeetCode刷题 (Python) | 155. Min Stack

LeetCode刷题 | 155. Min Stack
  • Titan0427
  • Titan0427
  • 2016-01-30 17:53
  • 1060

LeetCode(155) Min Stack

题目如下: 分析如下: 我的代码:
  • feliciafay
  • feliciafay
  • 2014-11-18 06:50
  • 6129

LeetCode 155 : Min Stack (Java)

解题思路:用java写的话有个需要注意的地方就是由于Stack中的元素用了泛型,而泛型不支持基本类型,所以需要基本类型的包装类。而包装类不同于基本类型的是包装类是引用,要比较相等不能简单使用==,要使用equals方法。 LeetCode官网unlock的提示: Hints:Consider ...
  • changetocs
  • changetocs
  • 2015-12-12 14:58
  • 1357

【leetcode c++】155 Min stack

Min stack Design a stack that supports push, pop,top, and retrieving the minimum element in constant time.   push(x) -- Push element x onto st...
  • hqq39
  • hqq39
  • 2015-10-09 17:55
  • 444

leetcode 155 Min Stack C++

用一个最小栈去维护最小值。 最小栈只入栈比当前值小的值。 因为在这个最小值后面进来的所有值中,最小值一直是这一个。 class MinStack { private: stack stk; stack min; public: /** initialize yo...
  • a2331046
  • a2331046
  • 2016-08-18 19:10
  • 192

【LeetCode-面试算法经典-Java实现】【155-Min Stack(最小栈)】

【155-Min Stack(最小栈)】【LeetCode-面试算法经典-Java实现】【所有题目目录索引】原题  Design a stack that supports push, pop, top, and retrieving the minimum element in constant ...
  • DERRANTCM
  • DERRANTCM
  • 2015-08-21 06:30
  • 3230

[leetcode] #155 Min Stack

[leetcode] #155 Min StackDesign a stack that supports push, pop, top, and retrieving the minimum element in constant time.push(x) – Push element x ont...
  • zhoujl25
  • zhoujl25
  • 2015-10-14 10:21
  • 312

leetcode——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() -- Re...
  • happyxuma1991
  • happyxuma1991
  • 2016-04-02 10:51
  • 94

leetcode(155). Min Stack

problem Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. push(x) – Push element x onto s...
  • PKU_Jade
  • PKU_Jade
  • 2017-08-10 21:11
  • 87

LeetCode(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() – Remov...
  • fly_yr
  • fly_yr
  • 2015-10-31 13:16
  • 305
    个人资料
    • 访问:19139次
    • 积分:643
    • 等级:
    • 排名:千里之外
    • 原创:45篇
    • 转载:1篇
    • 译文:0篇
    • 评论:6条
    文章分类
    最新评论