美文网首页
算法刷题之最小栈

算法刷题之最小栈

作者: mrwangyong | 来源:发表于2017-05-31 11:32 被阅读148次
  • 题目:
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.
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.

提示:提交代码后,需要用简洁的语言解释一下代码思路~ 谢谢

历史题目和总结见公众号「每日一道算法题」

https://leetcode.com/problems/min-stack/#/description
  • 解决思路:

    1. 主类:
public class TestStack {
  private Node top;
  private Integer min;

  public TestStack() {

  }

  public void push(int x) {
    min = (min == null || min > x) ? x : min;
    if (top == null) {
      top = new Node(x);
    } else {
      Node node = new Node(x);
      node.next = top;
      top = node;
    }
  }

  public void pop() {
    if (top != null) {
      int index = top.index;
      top = top.next;
      if (index == min) {
        min = findMin();
      }
    } else {
      throw new StackOverflowError();
    }
  }

  public int top() {
    if (top != null) {
      return top.index;
    } else {
      throw new StackOverflowError();
    }
  }

  public int getMin() {
    return min;
  }

  private Integer findMin() {
    if (top != null) {
      min = top.index;
      Node next = top.next;
      while (next != null) {
        Node node = next;
        if (min > node.index) {
          min = node.index;
        }
        next = node.next;
      }
    } else {
      min = null;
    }
    return min;
  }

  public static class Node {
    private int index;
    private Node next;

    public Node(int index) {
      this.index = index;
    }
  }

  @Override
  public String toString() {
    StringBuilder builder = new StringBuilder();
    if (top != null) {
      builder.append(top.index).append(",");
      Node next = top.next;
      while (next != null) {
        Node node = next;
        builder.append(node.index).append(",");
        next = node.next;
      }
    }
    return builder.toString();
  }
}
  • 测试类:
public class Test {
  public static void main(String[] args) {
    MinStack minStack = new MinStack<>();
    minStack.push(2147483646);
  minStack.push(2147483646);
  minStack.push(2147483647);

  minStack.top();
  minStack.pop();
  minStack.getMin();

  minStack.pop();
  minStack.getMin();
  minStack.pop();
  minStack.push(2147483647);
  int min = minStack.getMin();

  System.out.println("minStack min=" + min);
  }
}
  • 算法说明:

    1. Java中,实现栈操作的位Dqueue接口,但是这里我并没有直接拿来用,而是按照LinkList的链表来实现的,每个元素都是一个Node;并且每个元素都有向下的引用,具体结构如下,这也是链表的基本实现原理,即每个元素都持有身边两个元素的引用,使得想很多个孩子一样,手拉手...
image.png image.png
  1. 为了得到最大的程序效率,在push和pop的过程中,对最小值进行缓存处理,提高程序的响应速度;

相关文章

  • 算法刷题之最小栈

    题目: 解决思路:主类: 测试类: 算法说明:Java中,实现栈操作的位Dqueue接口,但是这里我并没有直接拿来...

  • 数据结构——栈和队列

    用数组实现栈和队列 用栈实现队列 用队列实现栈 栈和队列的经典算法题最小间距栈宠物收养所 数组实现栈和队列 用数组...

  • 栈系列之-获取最小值

    一、栈获取最小值算法概述 获取栈的最小值算法:可以动态的获取一个栈中元素的最小值,动态的意思是,当该栈发生push...

  • 96-不同的二叉搜索树-美丽的卡特兰数

    写在最前面 上题之前先写点前两天面试时面试官出的一道算法题的感想。他给的题目是实现一个栈,使得压栈、弹栈、求最小值...

  • 算法设计 --- 最小栈

    算法描述 设计一个支持 push ,pop ,top 操作,并能在常数时间内检索到最小元素的栈。 push(x) ...

  • LeetCode刷题之栈、队列

    栈(Stack) 又名堆栈,它是一种重要的数据结构。从数据结构角度看,栈也是线性表,其特殊性在于栈的基本操作是线性...

  • 学习计划

    以做出心目中的网站为目标,学习相关的技术栈可以先从微信小程序开始学起来 算法 算法数上的所有算法都应该熟知 多刷题...

  • [刷题防痴呆] 0155 - 最小栈 (Min Stack)

    题目地址 https://leetcode.com/problems/min-stack/description/...

  • 每日Leetcode—算法(14)

    141.环形链表 算法(快慢指针): 155.最小栈 算法一: 算法二: 167. 两数之和 II - 输入有序数...

  • 刷leetCode算法题+解析(十三)

    又是一个愉快的周五,继续刷题ing~ 最小栈 题目:设计一个支持 push,pop,top 操作,并能在常数时间内...

网友评论

      本文标题:算法刷题之最小栈

      本文链接:https://www.haomeiwen.com/subject/vdndfxtx.html