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; + } + } +} +``` +