当前位置:网站首页>Sword finger offer 30 Stack containing min function
Sword finger offer 30 Stack containing min function
2022-06-28 08:17:00 【wy_ forty-three million four hundred and thirty-one thousand ei】
The finger of the sword Offer 30. contain min Function of the stack
The difficulty is simple 272
Defines the data structure of the stack , Please implement a in this type that can get the minimum elements of the stack min The function is in the stack , call min、push And pop The time complexity of O(1).
Example :
MinStack minStack = new MinStack();
minStack.push(-2);
minStack.push(0);
minStack.push(-3);
minStack.min(); --> return -3.
minStack.pop();
minStack.top(); --> return 0.
minStack.min(); --> return -2.
class MinStack {
/** initialize your data structure here. */
Stack<Integer> stack1;
Stack<Integer> stack2;
public MinStack() {
stack1=new Stack<>();
stack2=new Stack<>();
}
// push(x) function : The focus is to keep the stack B The element is Not strictly descending Of .
// take x Push to stack A ( namely A.add(x) );
// if ① Stack B It's empty or ② x Less than or equal to Stack B Top element of , Will x Push to stack B ( namely B.add(x) ).
public void push(int x) {
stack1.add(x);
if(stack2.isEmpty() || x<=stack2.peek()){
stack2.add(x);
}
}
//pop() function : The focus is to keep the stack A, B Of Element consistency .
// Execution stack A Out of the stack ( namely A.pop() ), Record the out of stack element as y ;
// if y Equal stack B Top element of , Then execute stack B Out of the stack ( namely B.pop() )
public void pop() {
//stack1.pop(); Redundant eject
if(stack1.pop().equals(stack2.peek())){
stack2.pop();
}
}
//top() function : Directly back to the stack A The top element of the stack , Return A.peek() .
public int top() {
return stack1.peek();
}
//min() function : Directly back to the stack B The top element of the stack , Return B.peek() .
public int min() {
return stack2.peek();
}
}
/**
* Your MinStack object will be instantiated and called as such:
* MinStack obj = new MinStack();
* obj.push(x);
* obj.pop();
* int param_3 = obj.top();
* int param_4 = obj.min();
*/
边栏推荐
- redis02——一篇终结redis的五种数据类型操作命令(可学习、复习、面试、收藏备用)
- Redis uses sentinel master-slave switching. What should the program do?
- nlp序列完全可以模拟人脑智能
- [learning notes] matroid
- MySQL implements transaction persistence using redo logs
- Devops foundation chapter Jenkins deployment (II)
- Hidden scroll bar on PC
- 抗洪救灾,共克时艰,城联优品捐赠10万元爱心物资驰援英德
- Installing MySQL under Linux
- Oracle view tablespace usage
猜你喜欢

NLP sequence can completely simulate human brain intelligence

Configuring multiple instances of MySQL under Linux

The maximum number of Rac open file descriptors, and the processing of hard check failure

Uvcgan: unt vision transformer cycle-consistent Gan for unpropared image-to-image translation

Introduction, compilation, installation and deployment of Doris learning notes

Upgrade HDP spark to spark 2.4.8 without upgrading ambari

【学习笔记】最短路 +生成树

Set the encoding of CMD to UTF-8

Do you know TCP protocol (2)?

SLAM中常用的雅克比矩阵J
随机推荐
B_QuRT_User_Guide(30)
Eslint 语法监测关闭
Eslint syntax monitoring off
npm清理缓存
NLP sequence can completely simulate human brain intelligence
sql分析(查询截取分析做sql优化)
Study notes 22/1/17
PMP从报考到拿证基本操作,了解PMP必看篇
ROS 笔记(08)— 服务数据的定义与使用
MySQL tablespace parsing
你了解TCP協議嗎(二)?
B_QuRT_User_Guide(29)
Redis master-slave structure and application scenarios
设置cmd的编码为utf-8
【学习笔记】拟阵
关于如何在placeholder中使用字体图标
Is it reliable for securities companies to register and open accounts? Is it safe?
2022巴黎时装周儿童单元6.19武汉站圆满落幕
About RAC modifying scan IP
How redis solves cache avalanche, breakdown and penetration problems