当前位置:网站首页>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
*/边栏推荐
- Yan reports an error: exception message: /bin/bash: line 0: fg: no job control
- 数组相关的知识点
- samba服务器配置
- 11_ue4进阶_男性角色换成女性角色,并修改动画
- 一文学会如何做电商数据分析(附运营分析指标框架)
- GKCircleObstacle
- 盘点:令人心动的数据可视化图表
- nodemcu之开发环境配置
- Blue Bridge Cup embedded Hal library ADC
- Pyqt5 rapid development and practice 4.13 menu bar, toolbar and status bar and 4.14 qprinter
猜你喜欢

10_ue4进阶_添加倒地和施法动作

I don't know how lucky the boy who randomly typed logs is. There must be a lot of overtime

剑指 Offer 06. 从尾到头打印链表

19. Delete the penultimate node of the linked list

GKCylindersNoiseSource

两年CRUD,二本毕业,备战两个月面试阿里,侥幸拿下offer定级P6

Blue Bridge Cup embedded Hal library USART_ TX
c语言进阶篇:指针(一)

Batch Normlization

ICML 2022 | graph represents the structure aware transformer model of learning
随机推荐
Operation log of dbeaver
Go memory model (version on May 31st, 2014)
Andorid development III (intent)
GKCheckerboardNoiseSource
Network file system service (NFS)
float浮动初步理解
GKRidgedNoiseSource
CRM+零代码:轻松实现企业信息化
GKRandomSource
GKVoronoiNoiseSource
GKPerlinNoiseSource
GKCylindersNoiseSource
Samba server configuration
02.1.2. logic type bool
Yan reported an error: could not find any valid local directory for nmprivate/
OCR 知识 概括
nodemcu之开发环境配置
数组相关的知识点
Blue Bridge Cup embedded Hal library ADC
适合中小企业的进销存软件,搞定5大难题