当前位置:网站首页>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();
}
}边栏推荐
- CVPR 2022 | single step 3D target recognizer based on sparse transformer
- RHCSA2
- 研究:数据安全工具在 60% 的情况下无法抵御勒索软件
- Wechat enterprise payment to change access, open quickly
- MySQL 巨坑:update 更新慎用影响行数做判断!!!
- Time conversion error
- Introduction to the principle of DNS
- 石臻臻的2021总结和2022展望 | 文末彩蛋
- Kotlin流程控制、循环
- Making and using the cutting tool of TTF font library
猜你喜欢

Iterator details in list... Interview pits

Annotation problem and hidden Markov model

Taobao short videos are automatically released in batches without manual RPA open source

上午面了个腾讯拿 38K 出来的,让我见识到了基础的天花

【Nacos云原生】阅读源码第一步,本地启动Nacos

Transactions from December 27 to 28, 2021

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

RHCSA1

你的下一台电脑何必是电脑,探索不一样的远程操作

Shi Zhenzhen's 2021 summary and 2022 outlook | colorful eggs at the end of the article
随机推荐
上午面了个腾讯拿 38K 出来的,让我见识到了基础的天花
SAP SEGW 事物码里的 Association 建模方式
Distributed solution - completely solve website cross domain requests
Install rhel8.2 virtual machine
激动人心!2022开放原子全球开源峰会报名火热开启!
Lepton 无损压缩原理及性能分析
A deep long article on the simplification and acceleration of join operation
GPON other manufacturers' configuration process analysis
Transactions from December 29, 2021 to January 4, 2022
SAP SEGW 事物码里的导航属性(Navigation Property) 和 EntitySet 使用方法
C alarm design
RHCSA5
Keras implements verification code identification
[cloud native] event publishing and subscription in Nacos -- observer mode
Free testing of Taobao tmall API order and flag insertion remark interface
NFT: how to make money with unique assets?
Kotlin function
深度长文探讨Join运算的简化和提速
Database connection pool & jdbctemplate
关于 SAP UI5 floating footer 显示与否的单步调试以及使用 SAP UI5 的收益