当前位置:网站首页>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 from entry to earth (8) array
- [error record] no matching function for call to 'cacheflush' cacheflush();)
- DM8 database recovery based on point in time
- From scratch, use Jenkins to build and publish pipeline pipeline project
- Leetcode topic [array] - 121 - the best time to buy and sell stocks
- DM8 uses different databases to archive and recover after multiple failures
- Awk from digging into the ground to getting started (10) awk built-in functions
- 保姆级JDEC增删改查练习
- awk从入门到入土(12)awk也可以写脚本,替代shell
- Manjaro install wechat
猜你喜欢

ctfshow web255 web 256 web257

How to re enable local connection when the network of laptop is disabled

AI Winter Olympics | is the future coming? Enter the entrance of the meta universe - virtual digital human

Awk from entry to earth (12) awk can also write scripts to replace the shell

How to solve the problem that computers often flash
![Private collection project practice sharing [Yugong series] February 2022 U3D full stack class 007 - production and setting skybox resources](/img/f9/6c97697896cd1bd0f1d62542959f29.jpg)
Private collection project practice sharing [Yugong series] February 2022 U3D full stack class 007 - production and setting skybox resources

Codeforces Round #803 (Div. 2)(A-D)

Guanghetong's high-performance 4g/5g wireless module solution comprehensively promotes an efficient and low-carbon smart grid

What exactly is DAAS data as a service? Don't be misled by other DAAS concepts

System disk expansion in virtual machine
随机推荐
Developers really review CSDN question and answer function, and there are many improvements~
awk从入门到入土(6)正则匹配
20220701 barbarat lemma proof
2022 examination questions for safety managers of metal and nonmetal mines (underground mines) and examination papers for safety managers of metal and nonmetal mines (underground mines)
Review of last week's hot spots (6.27-7.3)
C # implements a queue in which everything can be sorted
awk从入门到入土(14)awk输出重定向
awk从入门到入土(18)gawk线上手册
Basic discipline formula and unit conversion
Awk from entry to soil (5) simple condition matching
In depth investigation and Strategic Research Report on China's motion controller Market (2022 Edition)
In depth research and investment strategy report on China's hydraulic parts industry (2022 Edition)
The basic syntax of mermaid in typera
Basic operations of databases and tables ----- view data tables
awk从入门到入土(4)用户自定义变量
Educational Codeforces Round 115 (Rated for Div. 2)
C语言-入门-基础-语法-[变量,常亮,作用域](五)
awk从入土到入门(10)awk内置函数
老掉牙的 synchronized 锁优化,一次给你讲清楚!
Live in a dream, only do things you don't say