From a572ef86933999a790ad8cc0ecefa515a336a4a0 Mon Sep 17 00:00:00 2001 From: Omooo <869759698@qq.com> Date: Mon, 29 Jun 2020 09:20:15 +0800 Subject: [PATCH] =?UTF-8?q?Update=20=E6=A0=88=E7=9B=B8=E5=85=B3.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- blogs/Algorithm/剑指 Offer/栈相关.md | 45 +++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/blogs/Algorithm/剑指 Offer/栈相关.md b/blogs/Algorithm/剑指 Offer/栈相关.md index e56869d..f3c13c8 100644 --- a/blogs/Algorithm/剑指 Offer/栈相关.md +++ b/blogs/Algorithm/剑指 Offer/栈相关.md @@ -33,3 +33,48 @@ class CQueue { } ``` +#### [30. 包含min函数的栈](https://leetcode-cn.com/problems/bao-han-minhan-shu-de-zhan-lcof/) + +```java +class MinStack { + + private Node head; + + public MinStack() { + } + + public void push(int x) { + if (head == null) { + head = new Node(x, x, null); + } else { + Node next = head; + head = new Node(x, Math.min(x, next.min), next); + } + } + + public void pop() { + head = head.next; + } + + public int top() { + return head.val; + } + + public int min() { + return head.min; + } + + class Node { + public int val; + public int min; + public Node next; + + public Node(int val, int min, Node next) { + this.val = val; + this.min = min; + this.next = next; + } + } +} +``` +