当前位置:网站首页>LeetCode-155-最小栈
LeetCode-155-最小栈
2022-06-11 21:23:00 【z754916067】
题目

思路
- 关键问题在于:如果pop掉的是最小值,那么该如何查找pop掉后栈中最小的值?
- 考虑用两个栈试一下?结果卡时间了,润去题解。
- 题解是每次和栈顶比较,push进每次比较的最小值,这样删除的时候可以直接pop了,学会了。
代码
static class MinStack {
Deque<Integer> xStack;
Deque<Integer> minStack;
public MinStack() {
//底层用的链表存储
xStack = new LinkedList<Integer>();
minStack = new LinkedList<Integer>();
minStack.push(Integer.MAX_VALUE);
}
public void push(int x) {
xStack.push(x);
//每次push都会找当前栈顶对应的最小值存到栈中
minStack.push(Math.min(minStack.peek(), x));
}
public void pop() {
xStack.pop();
minStack.pop();
}
public int top() {
return xStack.peek();
}
public int getMin() {
return minStack.peek();
}
}
边栏推荐
- 俩月没发过博客了,发一篇证明自己的账号还活着
- BUG -- coredump使用
- Tensorflow 2. X Getting Started tutorial
- Educational Codeforces Round 111 (Rated for Div. 2) C 补题
- Solve the problem of img 5px spacing
- 字符串复制函数
- JVM|本地方法接口;本地方法栈
- go语言的goto语句
- JS performs non empty judgment on various data types of the returned data.
- Leetcode 797. All possible paths
猜你喜欢

JS performs non empty judgment on various data types of the returned data.

Codeforces Round #744 (Div. 3) 解题报告

JVM object allocation policy TLAB

Deriving Kalman filter from probability theory

How to Load Data from CSV (Data Preparation Part)

Answer fans' questions | count the number and frequency of letters in the text

Online excel file parsing and conversion to JSON format

关于斜率优化

JMeter load test finds the maximum number of concurrent users (including step analysis)

One article to show you how to understand the harmonyos application on the shelves
随机推荐
Cs144 lab0 lab1 record
[Part 15] use and basic principle of forkjoinpool [key]
go语言的goto语句
JVM heap
关于斜率优化
How to manually send events exposed by SAP commerce cloud mock application using SAP kyma console
一个Golang的私有库设置问题
Solution to the problem of PHP strtotime obtaining natural monthly error
In idea, run the yarn command to show that the file cannot be loaded because running scripts is disabled on this system
Goto statement of go language
字符串复制函数
正则校验匹配[0-100]、[0-1000]之间的正整数或小数点位数限制
Use float to create a page header, footer, left content, and main content.
Codeforces Round #744 (Div. 3) 解题报告
2022年6月9日 16:29:41 日记
如何使用 SAP Kyma 控制台手动发送 SAP Commerce Cloud Mock 应用暴露的事件
Codeforces Round #739 (Div. 3)解题报告
应用业务层修改
【生活思考】文字与语音
如何创建最简单的 SAP Kyma Function