To maintain a stack and maintain the minimum stack of elements getmin__ stack

Source: Internet
Author: User

GITHUB:HTTPS://GITHUB.COM/CBAMLS
Welcome to join (Java) National Schools IT elite QQ Group number: 467123855

Package com.offer.chapter1;

Import Java.util.Stack; /** * Copyrright (c) 2014-2016 haerbin hearglobal co.,ltd * Project:demo * Comments: * Author:cbam * Create date:2017 /2/4 * Modified by: * Modified Date: * Modified Reason: */public class Problem_01_mystack1 {private Stack<int
    Eger> Stackdata;

    Private stack<integer> stackmin;
        Public Problem_01_mystack1 () {this.stackdata = new stack<> ();
    This.stackmin = new stack<> (); public int Getmin () {if (This.stackMin.isEmpty ()) {throw new RuntimeException (' Your stack is E
        Mpty ");
    return This.stackMin.peek ();
        public void push (int newnum) {if (This.stackMin.isEmpty ()) {This.stackMin.push (newnum);
        else if (Newnum <= this.getmin ()) {This.stackMin.push (newnum);
    } this.stackData.push (Newnum);
        public int Pop () {if (This.stackData.isEmpty ()) {    throw new RuntimeException ("Your stack is empty");
        int value = This.stackData.pop ();
        if (value = = This.getmin ()) {This.stackMin.pop ();
    return value; }
}

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.