Topic Description
To define the data structure of the stack, implement a min function that gets the minimum stack element in the type.
Found a lot of Java code than C + + short, Meow ~
Implement functions of several functions
Create a new auxiliary stack, so that the secondary stack from top to bottom storage should be the minimum value of-> secondary small value-> ... class Solution {public: stack<int>
Datastack, Minstack; void push (int val) {//press the element into the stack where the data is stored datasta
Ck.push (Val); When the stack of the smallest element is stored without elements, the current element is pressed into the minimum element stack.
Otherwise, first compare the size, press the smaller element into the smallest element stack. if (Minstack.empty ()) {
minstack.push (Val); &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP} else{
int min = Minstack.top (); val<=min?minstack.push (val):
Minstack.push (min); }
&NBSP;&NBSP;&NBSP}//pop, the data stack auxiliary stack pops up the top element. &Nbsp;void pop () { datastack.pop ();
minstack.pop (); &NBSP;&NBSP;&NBSP;&NBSP} int Top () {
return Datastack.top (); &NBSP;&NBSP;&NBSP;&NBSP} int min () {
return Minstack.top (); };