当前位置:网站首页>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
边栏推荐
- Sequence model
- Educational Codeforces Round 115 (Rated for Div. 2)
- 09 softmax regression + loss function
- C语言-入门-基础-语法-数据类型(四)
- Horizon sunrise X3 PI (I) first boot details
- In depth investigation and Strategic Research Report on China's motion controller Market (2022 Edition)
- The map set type is stored in the form of key value pairs, and the iterative traversal is faster than the list set
- awk从入门到入土(8)数组
- Codeforces Round #793 (Div. 2)(A-D)
- CLion-控制台输出中文乱码
猜你喜欢
System disk expansion in virtual machine
Horizon sunrise X3 PI (I) first boot details
Call Baidu map to display the current position
Relationship and operation of random events
MySQL relearn 1-centos install mysql5.7
C語言-入門-基礎-語法-[運算符,類型轉換](六)
Clion console output Chinese garbled code
ctfshow web255 web 256 web257
C语言-入门-基础-语法-[变量,常亮,作用域](五)
DM8 command line installation and database creation
随机推荐
Awk from entry to earth (12) awk can also write scripts to replace the shell
Codeforces Round #793 (Div. 2)(A-D)
Research Report on research and investment prospects of China's testing machine industry (2022 Edition)
【LeetCode 42】501. Mode in binary search tree
How to solve the problem that computers often flash
DM8 uses different databases to archive and recover after multiple failures
China electronic grade sulfur trioxide Market Forecast and investment strategy report (2022 Edition)
如何通过antd的upload控件,将图片以文件流的形式发送给服务器
20220701 barbarat lemma proof
Educational Codeforces Round 119 (Rated for Div. 2)
上周热点回顾(6.27-7.3)
Codeforces Global Round 21(A-E)
Clion console output Chinese garbled code
What exactly is DAAS data as a service? Don't be misled by other DAAS concepts
How to send pictures to the server in the form of file stream through the upload control of antd
Research Report on the current market situation and development prospects of calcium sulfate whiskers in China (2022 Edition)
awk从入门到入土(5)简单条件匹配
Codeforces Round #793 (Div. 2)(A-D)
C语言-入门-基础-语法-[主函数,头文件](二)
ArcGIS应用(二十二)Arcmap加载激光雷达las格式数据