leetcode#155 Min Stack

Source: Internet
Author: User

Original title Address

It's a very classic question. Double Stack method, a normal stack, a minimum value stack

In the stack: in addition to the normal stack, if the current stack element is less than or equal to the minimum stack top element, then simultaneously into the minimum value stack

When out of stack: In addition to the normal stack, if the current stack element is less than or equal to the minimum stack top element, then the minimum stack

View top of Stack element: Return to normal stack top element

View minimum element: Returns the top element of the minimum value stack

Code:

1stack<int>St;2stack<int>Minst;3 4 voidPushintx) {5 St.push (x);6   if(Minst.empty () | | | x <=minst.top ())7 Minst.push (x);8 }9 Ten voidpop () { One   if(St.empty ())return; A   if(St.top () = =minst.top ()) - Minst.pop (); - St.pop (); the } -  - intTop () { -   if(St.empty ())return-1; +   Else returnst.top (); - } +  A intgetmin () { at   if(St.empty ())return-1; -   Else returnminst.top (); -}

leetcode#155 Min Stack

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.