当前位置:网站首页>155. 最小栈
155. 最小栈
2022-07-05 12:39:00 【厚积薄发ض】
设计一个支持 push ,pop ,top 操作,并能在常数时间内检索到最小元素的栈。
实现 MinStack 类:
- MinStack() 初始化堆栈对象。
- void push(int val) 将元素val推入堆栈。
- void pop() 删除堆栈顶部的元素。
- int top() 获取堆栈顶部的元素。
- int getMin() 获取堆栈中的最小元素。
输入:
["MinStack","push","push","push","getMin","pop","top","getMin"]
[[],[-2],[0],[-3],[],[],[],[]]输出:
[null,null,null,null,-3,null,0,-2]解释:
MinStack minStack = new MinStack();
minStack.push(-2);
minStack.push(0);
minStack.push(-3);
minStack.getMin(); --> 返回 -3.
minStack.pop();
minStack.top(); --> 返回 0.
minStack.getMin(); --> 返回 -2.
题意:这个题题意就是得到栈中最小的数字,不管你是入栈还是出栈之后最终得到栈中的最小数字。
思路:准备两个栈,一个栈是正常的栈,一个栈是保存栈中最小数字的的栈,我们也叫他最小栈,当两个栈刚开始为空时,我们就将元素同时入到这两个栈中,如果不为空,将元素入到第一个普通栈中,然后用这个元素与最小栈栈顶元素比较,如果大于最小栈栈顶元素就不入最小栈,如果小于最小栈栈顶元素就入最小栈。
class MinStack {
Stack<Integer> stack;
Stack<Integer> minStack;
public MinStack() {//初始化两个栈
stack = new Stack<>();//普通栈
minStack = new Stack<>();//最小栈
}
public void push(int val) {
if(stack.empty()&&minStack.empty()){
//两个栈都为空,同时push元素
stack.push(val);
minStack.push(val);
}else {
stack.push(val);
if(val<=minStack.peek()){//比最小栈栈顶元素小就入
minStack.push(val);
}
}
}
public void pop() {//出栈,如果出的是与最小栈栈顶元素相同,那么也同时出最小栈栈顶元素,保持最小栈栈顶依然是栈中最小的元素
int x = stack.pop();
if(x==minStack.peek()){
minStack.pop();
}
}
public int top() {//弹出普通栈栈顶元素
return stack.peek();
}
public int getMin() {//最小栈栈顶元素就是最小值
return minStack.peek();
}
}边栏推荐
- Detailed structure and code of inception V3
- Alipay transfer system background or API interface to avoid pitfalls
- Storage Basics
- Simply take stock reading notes (1/8)
- Keras implements verification code identification
- Neural network of PRML reading notes (1)
- 实战模拟│JWT 登录认证
- Distance measuring sensor chip 4530a used in home intelligent lighting
- Wechat enterprise payment to change access, open quickly
- 深度长文探讨Join运算的简化和提速
猜你喜欢

石臻臻的2021总结和2022展望 | 文末彩蛋

Distance measuring sensor chip 4530a used in home intelligent lighting

Transactions from January 14 to 19, 2022

A few years ago, I outsourced for four years. Qiu Zhao felt that life was like this

Taobao, pinduoduo, jd.com, Doudian order & Flag insertion remarks API solution

非技术部门,如何参与 DevOps?

Notes for preparation of information system project manager --- information knowledge

Comprehensive upgrade of Taobao short video photosynthetic platform

SAP UI5 DynamicPage 控件介绍

UNIX socket advanced learning diary - advanced i/o functions
随机推荐
国内市场上的BI软件,到底有啥区别
2021.12.16-2021.12.20 empty four hand transaction records
stm32和电机开发(从架构图到文档编写)
Pytoch loads the initialization V3 pre training model and reports an error
VoneDAO破解组织发展效能难题
Transactions from January 14 to 19, 2022
Four common problems of e-commerce sellers' refund and cash return, with solutions
Tips and tricks of image segmentation summarized from 39 Kabul competitions
SAP UI5 视图里的 OverflowToolbar 控件
SAP UI5 FlexibleColumnLayout 控件介绍
Difference between JUnit theories and parameterized tests
Why is your next computer a computer? Explore different remote operations
Laravel文档阅读笔记-mews/captcha的使用(验证码功能)
Taobao, pinduoduo, jd.com, Doudian order & Flag insertion remarks API solution
Database connection pool & jdbctemplate
【Nacos云原生】阅读源码第一步,本地启动Nacos
Simply take stock reading notes (3/8)
[cloud native] event publishing and subscription in Nacos -- observer mode
在家庭智能照明中应用的测距传感芯片4530A
《信息系统项目管理师》备考笔记---信息化知识