当前位置:网站首页>Sword finger offer 30 contains the stack of Min function
Sword finger offer 30 contains the stack of Min function
2022-07-04 08:55:00 【Sean_ Asu】
Still stack , Because I did it first 09, So when looking at this question , Priority has been given to the use of dual stacks , Similarly, the first stack is the data stack , The second stack is the auxiliary stack ( Smallest stack )
The comment of this question is in the solution , Animation given by the boss 、 Vernacular inscriptions are more popular 、 detailed , Here I write it simply , If you can understand
First Understand data stack and auxiliary stack ( Smallest stack ) The role of , seeing the name of a thing one thinks of its function , Data stack means that all elements are pushed into the data stack , The minimum stack is the minimum value of the element in the data stack after each input element in the data stack , As shown in the figure
When the initial , Push elements into the data stack 2

After pressing , Because there is only one element in the data stack 2 , Then the smallest element in the smallest stack is 2

Push in an element again 3 , At this time, there is 2、3 Two elements

At this time, the minimum stack is the minimum value of the element in the data stack , At this time, there is only 2、3 Two elements , The minimum value is 2, Then continue to push elements into the minimum stack 2

If you continue to push elements into the data stack 1 , So at this time The data stack element is :2、3、1

Then the minimum value of the three elements in the data stack becomes 1, So the data pushed into the minimum stack is 1

Through such steps, we can find , The top element of the smallest stack is always the smallest element in the data stack
So we can judge , To write push Methods
and pop Double stack elements pop
top Then the top element of the data stack is returned
min According to the red summary above , Is returned The top element of the smallest stack
Write the code according to the idea
class MinStack { Stack<Integer> data_stack; Stack<Integer> min_stack; /** initialize your data structure here. */ public MinStack() { data_stack = new Stack<>(); min_stack = new Stack<>(); } public void push(int x) { if(min_stack.isEmpty()){ // The minimum stack is empty , It indicates that the data stack is also empty , Then press the element into the minimum stack min_stack.push(x); }else{ // Minimum stack is not empty , Note that the data stack is not empty , And the smallest stack top element is the smallest element of the data stack , Then judge and compare if(min_stack.peek() > x){ // The top element of the smallest stack > x , explain x smaller , It should be pushed into the minimum stack min_stack.push(x); }else{ // The top element of the smallest stack < x , explain x Bigger , Then the smallest stack is pressed into the smallest element recorded last min_stack.push(min_stack.peek()); } } data_stack.push(x); } public void pop() { data_stack.pop(); min_stack.pop(); } public int top() { return data_stack.peek(); } public int min() { return min_stack.peek(); } }
There needs to be a difference pop() and peek()
pop() Is the element that returns to the top of the stack , And delete it in the process .
peek() Is to return the element at the top of the stack , But don't delete it from the stack .
isEmpty() Is to judge whether it is empty , If the stack is empty , Then return to true, If the stack contains elements , Then return to false
边栏推荐
- awk从入门到入土(8)数组
- Research Report on research and investment prospects of China's testing machine industry (2022 Edition)
- [untitled] forwarding least square method
- C language - Introduction - Foundation - syntax - [operators, type conversion] (6)
- C语言-入门-基础-语法-[标识符,关键字,分号,空格,注释,输入和输出](三)
- NewH3C——ACL
- 埃氏筛+欧拉筛+区间筛
- Awk from digging into the ground to getting started (10) awk built-in functions
- FOC control
- awk从入门到入土(11)awk getline函数详解
猜你喜欢

Go zero micro service practical series (IX. ultimate optimization of seckill performance)

Educational Codeforces Round 119 (Rated for Div. 2)

Horizon sunrise X3 PI (I) first boot details

微服務入門:Gateway網關
](/img/89/0f5f4ba07c637b09abe873016cba2d.png)
C语言-入门-基础-语法-[标识符,关键字,分号,空格,注释,输入和输出](三)

ES6 summary

L1 regularization and L2 regularization

The basic syntax of mermaid in typera
![C language - Introduction - Foundation - syntax - [main function, header file] (II)](/img/5a/c6a3c5cd8038d17c5b0ead2ad52764.png)
C language - Introduction - Foundation - syntax - [main function, header file] (II)

Démarrage des microservices: passerelle
随机推荐
Display Chinese characters according to numbers
How to re enable local connection when the network of laptop is disabled
Research and investment strategy report of China's electronic hydrogen peroxide industry (2022 Edition)
Educational Codeforces Round 115 (Rated for Div. 2)
C语言-入门-基础-语法-[变量,常亮,作用域](五)
LinkedList in the list set is stored in order
DM database password policy and login restriction settings
ArcGIS application (XXII) ArcMap loading lidar Las format data
Codeforces Round #803 (Div. 2)(A-D)
2022 tower crane driver examination and tower crane driver examination questions and analysis
Basic discipline formula and unit conversion
C語言-入門-基礎-語法-[運算符,類型轉換](六)
How to send pictures to the server in the form of file stream through the upload control of antd
How to choose solid state hard disk and mechanical hard disk in computer
[BSP video tutorial] stm32h7 video tutorial phase 5: MDK topic, system introduction to MDK debugging, AC5, AC6 compilers, RTE development environment and the role of various configuration items (2022-
awk从入门到入土(15)awk执行外部命令
C#,数值计算(Numerical Recipes in C#),线性代数方程的求解,Gauss-Jordan消去法,源代码
Sequence model
C, Numerical Recipes in C, solution of linear algebraic equations, Gauss Jordan elimination method, source code
微服务入门:Gateway网关