mirror of
https://github.com/CyC2018/Interview-Notebook.git
synced 2026-10-30 18:23:52 +00:00
Update 30. 包含 min 函数的栈.md
题目链接改为力扣中的对应题目,题目描述和代码按该题的接口调整。
This commit is contained in:
+24
-16
@@ -2,7 +2,7 @@
|
||||
|
||||
## 题目链接
|
||||
|
||||
[牛客网](https://www.nowcoder.com/practice/4c776177d2c04c2494f2555c9fcc1e49?tpId=13&tqId=11173&tPage=1&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking&from=cyc_github)
|
||||
[Leetcode](https://leetcode.cn/problems/bao-han-minhan-shu-de-zhan-lcof/)
|
||||
|
||||
## 题目描述
|
||||
|
||||
@@ -15,24 +15,32 @@
|
||||
<div align="center"> <img src="https://cs-notes-1256109796.cos.ap-guangzhou.myqcloud.com/image-20201104013936126.png" width="350px"> </div><br>
|
||||
|
||||
```java
|
||||
private Stack<Integer> dataStack = new Stack<>();
|
||||
private Stack<Integer> minStack = new Stack<>();
|
||||
class MinStack {
|
||||
|
||||
public void push(int node) {
|
||||
dataStack.push(node);
|
||||
minStack.push(minStack.isEmpty() ? node : Math.min(minStack.peek(), node));
|
||||
}
|
||||
private Stack<Integer> dataStack;
|
||||
private Stack<Integer> minStack;
|
||||
|
||||
public void pop() {
|
||||
dataStack.pop();
|
||||
minStack.pop();
|
||||
}
|
||||
public MinStack() {
|
||||
dataStack = new Stack<>();
|
||||
minStack = new Stack<>();
|
||||
}
|
||||
|
||||
public int top() {
|
||||
return dataStack.peek();
|
||||
}
|
||||
public void push(int x) {
|
||||
dataStack.push(x);
|
||||
minStack.push(minStack.isEmpty() ? x : Math.min(minStack.peek(), x));
|
||||
}
|
||||
|
||||
public int min() {
|
||||
return minStack.peek();
|
||||
public void pop() {
|
||||
dataStack.pop();
|
||||
minStack.pop();
|
||||
}
|
||||
|
||||
public int top() {
|
||||
return dataStack.peek();
|
||||
}
|
||||
|
||||
public int getMin() {
|
||||
return minStack.peek();
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Reference in New Issue
Block a user