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