当前位置:网站首页>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
边栏推荐
- Codeforces Round #803 (Div. 2)(A-D)
- Cancel ctrl+alt+delete when starting up
- 没有Kubernetes怎么玩Dapr?
- How can we make a monthly income of more than 10000? We media people with low income come and have a look
- MySQL relearn 1-centos install mysql5.7
- 1211 or chicken and rabbit in the same cage
- manjaro安装微信
- C语言-入门-基础-语法-数据类型(四)
- C, Numerical Recipes in C, solution of linear algebraic equations, Gauss Jordan elimination method, source code
- C语言-入门-基础-语法-[主函数,头文件](二)
猜你喜欢

How can we make a monthly income of more than 10000? We media people with low income come and have a look

How does Xiaobai buy a suitable notebook

Educational Codeforces Round 119 (Rated for Div. 2)

Educational Codeforces Round 119 (Rated for Div. 2)

HMS core helps baby bus show high-quality children's digital content to global developers

Codeforces Global Round 21(A-E)

Démarrage des microservices: passerelle

Fault analysis | MySQL: unique key constraint failure

地平线 旭日X3 PI (一)首次开机细节

埃氏筛+欧拉筛+区间筛
随机推荐
manjaro安装微信
Codeforces Global Round 21(A-E)
Developers really review CSDN question and answer function, and there are many improvements~
Awk from entry to soil (5) simple condition matching
DM8 tablespace backup and recovery
C语言-入门-基础-语法-[变量,常亮,作用域](五)
Call Baidu map to display the current position
DM database password policy and login restriction settings
C, Numerical Recipes in C, solution of linear algebraic equations, Gauss Jordan elimination method, source code
4 small ways to make your Tiktok video clearer
Comparison between sentinel and hystrix
【LeetCode 42】501. Mode in binary search tree
Turn: excellent managers focus not on mistakes, but on advantages
FRP intranet penetration, reverse proxy
Learn nuxt js
Awk from entry to earth (18) GAW K line manual
awk从入土到入门(10)awk内置函数
Ehrlich sieve + Euler sieve + interval sieve
Awk from entry to earth (12) awk can also write scripts to replace the shell
Live in a dream, only do things you don't say