The topic describes the data structure of the definition stack, which implements a min function that can get the smallest element in the stack. Analysis: Using two stacks, a normal access element, a stack to store the smallest elements, the code is as follows:
1 ImportJava.util.Stack;2 3 Public classSolution {4 5stack<integer> S1 =NewStack<integer>() ;6stack<integer> s2 =NewStack<integer>() ;7 Public voidPushintnode) {8 S1.push (node);9 if(S2.size () ==0| | node<S2.peek ()) {Ten S2.push (node); One } A Else - S2.push (S2.peek ()); - } the Public intTop () { - returnS1.peek (); - } - Public voidpop () { + if(S1.size () >0&&s2.size () >0){ - S1.pop (); + S2.pop (); A } at } - - Public intmin () { - if(S1.size () >0&&s2.size () >0){ - returnS2.peek (); - } in returnInteger.max_value; - } to}
"Sword Point offer" 13, containing the stack of min function