Update 栈相关.md

master
Omooo 4 years ago
parent 03a07e4fe3
commit a572ef8693
  1. 45
      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;
}
}
}
```

Loading…
Cancel
Save