当前位置:网站首页>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();
*/
边栏推荐
- 安装nrm后,使用nrm命令报错internal/validators.js:124 throw new ERR_INVALID_ARG_TYPE(name, ‘string‘, value)
- 【学习笔记】差分约束
- [learning notes] linear basis
- Is it reliable to open an account by digging money? Is it safe?
- 小艺人黄鑫洋受邀参加巴黎时装周儿童单元武汉站
- Devops Basics: Jenkins deployment and use (I)
- NPM clean cache
- Oracle view tablespace usage
- About ASM disk space full, clean up ASM disk
- 【学习笔记】模拟
猜你喜欢
Ambari (VII) --- ambari integrated hue4.2 document (valid for personal test)
redis02——一篇终结redis的五种数据类型操作命令(可学习、复习、面试、收藏备用)
抗洪救灾,共克时艰,城联优品捐赠10万元爱心物资驰援英德
sql主从复制搭建
找合适的PMP机构只需2步搞定,一查二问
Vagrant installation
In flood fighting and disaster relief, the city donated 100000 yuan of love materials to help Yingde
Why MySQL cannot insert Chinese data in CMD
关于在cmd中MySQL不能插中文数据的原因
Discussion on the application of GIS 3D system in mining industry
随机推荐
PLSQL installation under Windows
Generation and verification of JWT token
Eslint 语法监测关闭
Reverse mapping of anonymous pages
How to choose an account opening broker? Is it safe to open an account online?
Set the encoding of CMD to UTF-8
PMP从报考到拿证基本操作,了解PMP必看篇
B_QuRT_User_Guide(28)
Eslint syntax monitoring off
The maximum number of Rac open file descriptors, and the processing of hard check failure
B_ QuRT_ User_ Guide(29)
SQL Master slave Replication Build
The RAC cannot connect to the database normally after modifying the scan IP. The ora-12514 problem is handled
【js】-【节流、防抖函数】
Configuring MySQL multi instance master-slave synchronization for Linux
Chenglian premium products donated love materials for flood fighting and disaster relief to Yingde
MySQL single table access method
2022巴黎时装周儿童单元6.19武汉站圆满落幕
Introduction, compilation, installation and deployment of Doris learning notes
Children's unit of 2022 Paris fashion week ended successfully at Wuhan station on June 19