当前位置:网站首页>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();
}
}边栏推荐
- Transactions on December 23, 2021
- Pinduoduo flag insertion remarks API
- GPON other manufacturers' configuration process analysis
- RHCSA4
- 深度长文探讨Join运算的简化和提速
- Transactions from December 27 to 28, 2021
- Transactions from December 29, 2021 to January 4, 2022
- Simply take stock reading notes (3/8)
- Distributed cache architecture - cache avalanche & penetration & hit rate
- mysql拆分字符串做条件查询
猜你喜欢

SAP UI5 视图里的 OverflowToolbar 控件

stirring! 2022 open atom global open source summit registration is hot!

Vonedao solves the problem of organizational development effectiveness

Kotlin variable

Oppo Xiaobu launched Obert, a large pre training model, and promoted to the top of kgclue

SAP SEGW 事物码里的 ABAP Editor

初识Linkerd项目

Comprehensive upgrade of Taobao short video photosynthetic platform

Compilation principle reading notes (1/12)

CVPR 2022 | single step 3D target recognizer based on sparse transformer
随机推荐
About LDA model
RHCSA5
Add a new cloud disk to Huawei virtual machine
太方便了,钉钉上就可完成代码发布审批啦!
RHCSA4
A possible investment strategy and a possible fuzzy fast stock valuation method
[Nacos cloud native] the first step of reading the source code is to start Nacos locally
RHCSA3
10 minute fitness method reading notes (1/5)
开发者,云原生数据库是未来吗?
Taobao order interface | order flag remarks, may be the most stable and easy-to-use interface
Simply take stock reading notes (4/8)
Kotlin variable
关于 SAP UI5 floating footer 显示与否的单步调试以及使用 SAP UI5 的收益
2021.12.16-2021.12.20 empty four hand transaction records
Taobao product details API | get baby SKU, main map, evaluation and other API interfaces
10 minute fitness method reading notes (5/5)
JSON parsing error special character processing (really speechless... Troubleshooting for a long time)
Reshape the power of multi cloud products with VMware innovation
RHCSA2