Min Stack

1. Title

2. Http address

https://leetcode.com/problems/min-stack/

3. The question

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.

4 My code(AC)

  •  1 public class MinStack {
     2
     3     // Accepted
     4     public LinkedList<Integer> stack = new LinkedList<Integer>();
     5     public LinkedList<Integer> min_stack = new LinkedList<Integer>();
     6
     7       public void push(int x) {
     8
     9               stack.offerFirst(x);
    10
    11               if ( min_stack.isEmpty() || x < min_stack.peekFirst()) {
    12                   min_stack.offerFirst(x);
    13               }else{
    14                   min_stack.offerFirst(min_stack.peekFirst());
    15               }
    16       }
    17
    18      public void pop() {
    19             stack.pollFirst();
    20             min_stack.pollFirst();
    21      }
    22
    23         public int top() {
    24             return stack.peekFirst();
    25         }
    26
    27         public int getMin() {
    28                 return min_stack.peekFirst();
    29         }
    30 }
时间: 2024-10-28 20:12:44

Min Stack的相关文章

2016.6.1——Min Stack

Min Stack 本题收获: 1.可以利用两个栈操作. 2.栈的基本操作. 题目: 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

LeetCode: Min Stack 解题报告

Min Stack My Submissions Question Solution 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

[LeetCode] Min Stack 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 stack. pop() -- Removes the element on top of the stack. top() -- Get the top element. getMin() -- Retrieve the

用stack实现min stack

遇到个好玩的问题,就是用一个stack实现min stack,什么意思呢,就是我实现stack,但是能以O(1)的时间复杂度和空间复杂度去找到我stack里面的最小值. 常规的方法是:用一个变量存放当前最小值,但是会出现这种情况,就是当我的stack pop掉的值刚好是最小值时候,后面就没法知道当前的最小值了. 怎么办呢?可以考虑在push阶段做个改变,就是在我每次往stack里面push数据的时候,跟当前的最小值比较,如果比当前最小值还小的话,那么将当前最小值入栈,再把最小值修改为这个值.在p

Java [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() -- Removes the element on top of the stack. top() -- Get the top element. getMin() -- Retrieve the min

[LintCode] Min Stack 最小栈

Implement a stack with min() function, which will return the smallest number in the stack. It should support push, pop and min operation all in O(1) cost. Notice min operation will never be called if there is no number in the stack. Have you met this

[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 returns the minimum element? Push, pop and min should all operate in O(1) time. LeetCode上的原题,请参见我之前的博客Min Stack 最小栈.

LeetCode[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 stack. - pop() – Removes the element on top of the stack. - top() – Get the top element. - getMin() – Retrieve the minim

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

这是悦乐书的第177次更新,第179篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第36题(顺位题号是155).设计一个支持push,pop,top和在恒定时间内检索最小元素的堆栈. push(x) - 将元素x推入堆栈. pop() - 删除堆栈顶部的元素. top() - 获取顶部元素. getMin() - 检索堆栈中的最小元素. 例如: MinStack minStack = new MinStack(); minStack.push(-2); minSta

Min Stack leetcode

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 e