当前位置:网站首页>Leetcode-155: minimum stack
Leetcode-155: minimum stack
2022-07-25 20:38:00 【Chrysanthemum headed bat】
leetcode-155: Smallest stack
subject
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 .
Example 1:
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.

Problem solving
and The finger of the sword Offer 30: contain min Function of the stack equally
Method 1 : Auxiliary stack
class MinStack {
public:
stack<int> st1;
stack<int> st2;
MinStack() {
}
void push(int val) {
if(st1.empty()) st2.push(val);
else st2.push(min(val,st2.top()));
st1.push(val);
}
void pop() {
st1.pop();
st2.pop();
}
int top() {
return st1.top();
}
int getMin() {
return st2.top();
}
};
边栏推荐
- Docker builds redis cluster
- 【NOI模拟赛】字符串匹配(后缀自动机SAM,莫队,分块)
- [MCU] 51 MCU burning those things
- Vivo official website app full model UI adaptation scheme
- [advanced mathematics] [5] definite integral and its application
- Web crawler principle analysis "suggestions collection"
- [today in history] June 29: SGI and MIPS merged; Microsoft acquires PowerPoint developer; News corporation sells MySpace
- JS scope and scope chain
- 火山引擎项亮:机器学习与智能推荐平台多云部署解决方案正式发布
- [workplace rules] it workplace rules | poor performance
猜你喜欢

Introduction to several scenarios involving programming operation of Excel in SAP implementation project

leetcode-114:二叉树展开为链表
![[advanced mathematics] [4] indefinite integral](/img/4f/2aae654599fcc0ee85cb1ba46c9afd.png)
[advanced mathematics] [4] indefinite integral

Jmeter——接口测试

Network protocol: TCP part2

JMeter - interface test

test

【ONNX】pytorch模型导出成ONNX格式:支持多参数与动态输入

How to choose a microservice registration center?
![[advanced drawing of single cell] 07. Display of KEGG enrichment results](/img/60/09c5f44d64b96c6e4d57e5f426e4ed.png)
[advanced drawing of single cell] 07. Display of KEGG enrichment results
随机推荐
How to choose a microservice registration center?
程序的编译和运行
Apache Mina framework "suggestions collection"
Open source SPL enhances mangodb computing
Remote—实战
TGA file format (waveform sound file format)
增加 swap 空间
test
[tensorrt] trtexec tool to engine
[today in history] July 13: the father of database passed away; Apple buys cups code; IBM chip Alliance
Compilation and operation of program
Key network protocols in tcp/ip four layer model
网络协议:TCP Part2
MySQL date [plus sign / +] condition filtering problem
Cloud native, Intel arch and cloud native secret computing three sig online sharing! See you today | issues 32-34
RF, gbdt, xgboost feature selection methods "recommended collection"
During the interview, I was asked how to remove the weight of MySQL, and who else wouldn't?
How to realize reliable transmission with UDP?
[cloud native] use of Nacos taskmanager task management
【高等数学】【3】微分中值定理与导数的应用