当前位置:网站首页>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();
*/
边栏推荐
- [learning notes] simulation
- Usage record of Xintang nuc980: self made development board (based on nuc980dk61yc)
- Priority of JS operator
- Redis cluster deployment and application scenarios
- Airflow2 configuration windows azure SSO details based on oauth2 protocol
- B_QuRT_User_Guide(28)
- Configuring MySQL multi instance master-slave synchronization for Linux
- js取整的小技巧
- Introduction, compilation, installation and deployment of Doris learning notes
- 你了解TCP協議嗎(二)?
猜你喜欢

22/02/15 study notes

SQL analysis (query interception analysis for SQL optimization)

Two tips for block level elements

Prometheus + grafana + MySQL master-slave replication + host monitoring

Almost Union-Find(带权并查集)

redis02——一篇终结redis的五种数据类型操作命令(可学习、复习、面试、收藏备用)

Connaissez - vous le protocole TCP (2)?

微内核Zephyr获众多厂家支持!

Eslint syntax monitoring off

App automated testing appium tutorial 2 - ADB command
随机推荐
11grac turn off archive log
Little artist huangxinyang was invited to participate in the Wuhan station of children's unit of Paris Fashion Week
小艺人黄鑫洋受邀参加巴黎时装周儿童单元武汉站
Unity - use of API related to Pico development input system ---c
Unity 获取当前物体正前方,一定角度、距离的坐标点
On the solution of insufficient swap partition
MySQL implements transaction persistence using redo logs
Solve NPM err! Unexpected end of JSON input while parsing near
【学习笔记】模拟
NPM clean cache
新唐NUC980使用记录:自制开发板(基于NUC980DK61YC)
Vagrant installation
sql主從複制搭建
B_ QuRT_ User_ Guide(29)
Chenglian premium products donated love materials for flood fighting and disaster relief to Yingde
Airflow2.1.1 summary of the pits stepped on in actual combat!!
npm清理缓存
Redis uses sentinel master-slave switching. What should the program do?
redis02——一篇终结redis的五种数据类型操作命令(可学习、复习、面试、收藏备用)
设置网页的标题部分的图标