算法刷题之最小栈

  • 题目:
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的过程中,对最小值进行缓存处理,提高程序的响应速度;

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 31,780评论 18 399
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,026评论 19 139
  • 1.把二元查找树转变成排序的双向链表 题目: 输入一棵二元查找树,将该二元查找树转换成一个排序的双向链表。 要求不...
    曲终人散Li阅读 3,378评论 0 19
  • 课程介绍 先修课:概率统计,程序设计实习,集合论与图论 后续课:算法分析与设计,编译原理,操作系统,数据库概论,人...
    ShellyWhen阅读 2,368评论 0 3
  • 湖是静的,蓝天白云静静的织在这幅画卷上,静静地; 湖是动的,白日阳光,夜晚月光,被一针针缝在湖面; 湖是软的,微风...
    DaDa酱阅读 723评论 1 4