当前位置:网站首页>155. Minimum stack
155. Minimum stack
2022-07-05 12:54:00 【accumulate steadily ض】
Design a support push ,pop ,top operation , And can retrieve the stack of the smallest elements in a constant time .
Realization MinStack class :
- MinStack() Initialize stack object .
- void push(int val) Put the element val Push to stack .
- void pop() Delete the element at the top of the stack .
- int top() Get the element at the top of the stack .
- int getMin() Get the smallest element in the stack .
Input :
["MinStack","push","push","push","getMin","pop","top","getMin"]
[[],[-2],[0],[-3],[],[],[],[]]Output :
[null,null,null,null,-3,null,0,-2]explain :
MinStack minStack = new MinStack();
minStack.push(-2);
minStack.push(0);
minStack.push(-3);
minStack.getMin(); --> return -3.
minStack.pop();
minStack.top(); --> return 0.
minStack.getMin(); --> return -2.
The question : The meaning of this question is to get the smallest number in the stack , Whether you enter or exit the stack, you finally get the smallest number in the stack .
Ideas : Prepare two stacks , A stack is a normal stack , A stack is a stack that holds the smallest number in the stack , We also call him the minimum stack , When the two stacks are empty at first , We will put elements into these two stacks at the same time , If it's not empty , Put the element into the first ordinary stack , Then compare this element with the smallest stack top element , If it is larger than the minimum stack, the top element of the stack will not be put into the minimum stack , If it is less than the minimum stack, the top element of the stack will be put into the minimum stack .
class MinStack {
Stack<Integer> stack;
Stack<Integer> minStack;
public MinStack() {// Initialize two stacks
stack = new Stack<>();// Ordinary stack
minStack = new Stack<>();// Smallest stack
}
public void push(int val) {
if(stack.empty()&&minStack.empty()){
// Both stacks are empty , meanwhile push Elements
stack.push(val);
minStack.push(val);
}else {
stack.push(val);
if(val<=minStack.peek()){// If it is smaller than the minimum stack top element, enter
minStack.push(val);
}
}
}
public void pop() {// Out of the stack , If the output is the same as the minimum stack top element , Then at the same time, the smallest stack top element , Keep the minimum stack. The top of the stack is still the smallest element in the stack
int x = stack.pop();
if(x==minStack.peek()){
minStack.pop();
}
}
public int top() {// Pop up the common stack top element
return stack.peek();
}
public int getMin() {// The minimum stack top element is the minimum
return minStack.peek();
}
}
边栏推荐
- 实战模拟│JWT 登录认证
- 使用 jMeter 对 SAP Spartacus 进行并发性能测试
- Database connection pool & jdbctemplate
- insmod 提示 Invalid module format
- Distributed solution - completely solve website cross domain requests
- Setting up sqli lab environment
- What is the difference between Bi software in the domestic market
- Annotation problem and hidden Markov model
- Docker configures redis and redis clusters
- How do e-commerce sellers refund in batches?
猜你喜欢
RHCAS6
Transactions from January 6 to October 2022
实战模拟│JWT 登录认证
Database connection pool & jdbctemplate
SAP SEGW 事物码里的 ABAP Editor
A few years ago, I outsourced for four years. Qiu Zhao felt that life was like this
VoneDAO破解组织发展效能难题
SAP UI5 DynamicPage 控件介紹
SAP SEGW 事物码里的导航属性(Navigation Property) 和 EntitySet 使用方法
你的下一台电脑何必是电脑,探索不一样的远程操作
随机推荐
在家庭智能照明中应用的测距传感芯片4530A
JSON parsing error special character processing (really speechless... Troubleshooting for a long time)
Comprehensive upgrade of Taobao short video photosynthetic platform
[Nacos cloud native] the first step of reading the source code is to start Nacos locally
I'm doing open source in Didi
2021-12-22 transaction record
HiEngine:可媲美本地的云原生内存数据库引擎
关于 SAP UI5 getSAPLogonLanguage is not a function 的错误消息以及 API 版本的讨论
Volatile instruction rearrangement and why instruction rearrangement is prohibited
mysql拆分字符串做条件查询
Kotlin function
Notes for preparation of information system project manager --- information knowledge
RHCSA2
JXL notes
Free testing of Taobao tmall API order and flag insertion remark interface
【云原生】Nacos中的事件发布与订阅--观察者模式
JDBC -- use JDBC connection to operate MySQL database
Install rhel8.2 virtual machine
UNIX socket advanced learning diary - advanced i/o functions
HiEngine:可媲美本地的云原生内存数据库引擎