当前位置:网站首页>Sword finger offer 30. stack containing min function
Sword finger offer 30. stack containing min function
2022-07-28 10:54:00 【jjj34】
Title Description :

Knowledge point : Create a stack ,pop(),push(),peek(),Integer_MAX_VALUE
1. Create a stack ,pop(),push(),peek()
Reference resources : The finger of the sword Offer 09. Queues are implemented with two stacks _jcxj2934 The blog of -CSDN Blog
2.Interger_MAX_VALUE
When comparing the minimum and maximum values , Consider using Integer.MIN_VALUE and Integer.MAX_VALUE
Integer.MAX_VALUE Express int Can represent the maximum value of an integer
Integer.MIN_VALUE Represents the minimum value
Use :
min1=Integer.Max_VALUE;
min1 = Math.min(min,x);
max1=Integer.MIN_VALUE;
max1 = Math.max(max1,x);3. Code
class MinStack {
private Stack<Integer> st1;
private Stack<Integer> st2;
/** initialize your data structure here. */
public MinStack() {
// Two stacks
st1=new Stack<>();
st2=new Stack<>();
st2.push(Integer.MAX_VALUE);
}
public void push(int x) {
st1.push(x);
// min1=Math.min(min1,x);
// The minimum value should be equal to st2 Top comparison of
st2.push(Math.min(st2.peek(),x));
}
public void pop() {
st1.pop();
st2.pop();
}
public int top() {
return st1.peek();
}
public int min() {
return st2.peek();
}
}
/**
* Your MinStack object will be instantiated and called as such:
* MinStack obj = new MinStack();
* obj.push(x);
* obj.pop(); Just get to the top of the stack , Take a look at , Do not remove
* int param_3 = obj.top();
* int param_4 = obj.min();
*/
/**
Ideas : Go through it , Find the subscript of the minimum
Import all the elements on the subscript into the stack 2
pop()
Stack 2 Put the element of back on the stack 1
min Only return without removing
*/边栏推荐
猜你喜欢

MySQL Architecture Principle

Markdown to word or PDF

蓝桥杯嵌入式-HAL库-SYSTICK

Blue Bridge Cup embedded Hal library LCD

GKNoiseMap

Characteristics and installation of non relational database mongodb

GKCheckerboardNoiseSource

Crm+ zero code: easily realize enterprise informatization

零代码 | 轻松实现数据仓库建模,搭建BI看板

剑指 Offer 30. 包含min函数的栈
随机推荐
学会这些分析方法及模型,遇到问题不再没思路
零代码 | 轻松实现数据仓库建模,搭建BI看板
GKRidgedNoiseSource
剑指 Offer 06. 从尾到头打印链表
Product side data analysis thinking
GKRandom
MySQL Architecture Principle
Blue Bridge Cup embedded Hal library ADC
Yan reported an error: could not find any valid local directory for nmprivate/
Advanced C language: pointer (1)
哈希表的相关知识点
GKNoiseMap
10_ue4进阶_添加倒地和施法动作
Blue Bridge Cup embedded Hal library USART_ TX
GKCheckerboardNoiseSource
盘点:144个免费学习网站,全网最全资源合集
蓝桥杯嵌入式-HAL库-SYSTICK
Pyqt5 rapid development and practice 4.11 drag and clipboard
Start from scratch blazor server (2) -- consolidate databases
GKConstantNoiseSource