Leetcode Stack 155 225 232

Source: Internet
Author: User

Easy Stack, 155 take the smallest element, with two stacks, the second stack saves the smallest element of the current stack, and then pops up when the element pops up according to the situation:

Class Minstack {public
:
    /** Initialize your data structure here. *
    /Minstack () {
        
    }
    
    void push (int x) {
        s1.push_back (x);
        if (S2.empty () | | X<=getmin ())
        {
            s2.push_back (x);
        }
    }
    
    void Pop () {
        if (S1.back () ==getmin ())
        {
            s2.pop_back ();
        }
        S1.pop_back ();
    }
    
    int Top () {return
        s1.back ();   
    }
    
    int Getmin () {return
        s2.back ();
    }
    vector<int> S1;
    vector<int> S2;
};

/**
 * Your Minstack object would be instantiated and called as such:
 * minstack obj = new Minstack ();
 * Obj.push (x);
 * Obj.pop ();
 * int param_3 = Obj.top ();
 * int param_4 = Obj.getmin ();
 */


255 with Deque, two-way queues:

Class Mystack {public
:
    /** Initialize your data structure here. *
    /Mystack () {
        
    }
    
    /** Push element x o Nto stack. */
    void push (int x) {
        s1.push_back (x);
    }
    
    /** removes the element on top of the stack and returns that element. */
    int pop () {
        int temp=top ();
        S1.pop_back ();
        return temp;
    }
    
    /** get the top element. */
    int Top () {return
        s1.back ();
    }
    
    /** Returns Whether the stack is empty. * *
    bool Empty () {
        if (S1.empty ())
        {return
            true;
        }
        return false;
    }
    deque<int> s1;
};

/**
 * Your Mystack object would be instantiated and called as such:
 * mystack obj = new Mystack ();
 * Obj.push (x);
 * int param_2 = Obj.pop ();
 * int param_3 = Obj.top ();
 * BOOL Param_4 = Obj.empty ();
 */


232, with two stacks to implement a queue, easy to think is S2 cache, S1 is the main, push directly behind the S1 push, but the pop to the S2 first, and then back to the S1, where the fall can be less than one, direct pop (this code does not reflect):

Class Myqueue {public:/** Initialize your data structure. * * Myqueue () {}/** Push E Lement X to the back of the queue.
    */void push (int x) {s1.push (x); }/** removes the element from in front to queue and returns that element.
            */int pop () {while (!s1.empty ()) {S2.push (S1.top ());
        S1.pop ();
        int Temp=s2.top ();
        S2.pop ();
            while (!s2.empty ()) {S1.push (S2.top ());
        S2.pop ();
    return to temp; }/** get the front element.
            */int Peek () {while (!s1.empty ()) {S2.push (S1.top ());
        S1.pop ();
        int Temp=s2.top ();
            while (!s2.empty ()) {S1.push (S2.top ());
        S2.pop ();
    return to temp; }/** Returns whether the queue is empty.
        * * BOOL Empty () {if (S1.empty ()) {    return true;
    return false;
    } stack<int> S1;
Stack<int> S2;

};
 /** * Your Myqueue object would be instantiated and called as such: * myqueue obj = new Myqueue ();
 * Obj.push (x);
 * int param_2 = Obj.pop ();
 * int param_3 = Obj.peek ();
 * BOOL Param_4 = Obj.empty (); */


Another variant, S2 is responsible for the play, S1 is responsible for the push tail, one pour one, do not have to pour back, if the push, found elements are in the S2, then pour back, otherwise not; if POPs, elements in the S1 to the S2, otherwise not:

Class Myqueue {public:/** Initialize your data structure. * * Myqueue () {}/** Push E Lement X to the back of the queue.
        */void push (int x) {if (S2.empty ()) S1.push (x);
                else {while (!s2.empty ()) {S1.push (S2.top ());
            S2.pop ();
        } s1.push (x); }/** removes the element from in front to queue and returns that element.
        */int pop () {int temp;
            if (S1.empty ()) {temp=s2.top ();
        S2.pop ();
            else {int n=s1.size ();
                for (int i=0;i<n-1;i++) {S2.push (S1.top ());
            S1.pop ();
            } temp=s1.top ();
        S1.pop ();
    return to temp; }/** get the front element.
        */int peek () {int temp;
   if (S1.empty ()) {         Temp=s2.top ();
                else {while (!s1.empty ()) {S2.push (S1.top ());
            S1.pop ();
        } temp=s2.top ();
    return to temp; }/** Returns whether the queue is empty.
        * * BOOL Empty () {if (S1.empty () &&s2.empty ()) {return true;
    return false;
    } stack<int> S1;
Stack<int> S2;

};
 /** * Your Myqueue object would be instantiated and called as such: * myqueue obj = new Myqueue ();
 * Obj.push (x);
 * int param_2 = Obj.pop ();
 * int param_3 = Obj.peek ();
 * BOOL Param_4 = Obj.empty (); */


The simplest variant, as long as the push into the S1, pop, s2 elements from the S2 pop, if there is no element in the S2, and then the S1 elements are poured into the S2, and then pop, this fall of the least number of times.

Class Myqueue {public:/** Initialize your data structure. * * Myqueue () {}/** Push E Lement X to the back of the queue.
        
    */void push (int x) {s1.push (x); }/** removes the element from in front to queue and returns that element.
        */int pop () {int temp;
            if (!s2.empty ()) {temp=s2.top ();
        S2.pop ();
            else {int n=s1.size ();
                for (int i=0;i<n-1;i++) {S2.push (S1.top ());
            S1.pop ();
            } temp=s1.top ();
        S1.pop ();
    return to temp; }/** get the front element.
        */int peek () {int temp;
        if (!s2.empty ()) {temp=s2.top ();
                else {while (!s1.empty ()) {S2.push (S1.top ());
            S1.pop ();
       } temp=s2.top (); return to temp; }/** Returns whether the queue is empty.
        * * BOOL Empty () {if (S1.empty () &&s2.empty ()) {return true;
    return false;
    } stack<int> S1;
Stack<int> S2;

};
 /** * Your Myqueue object would be instantiated and called as such: * myqueue obj = new Myqueue ();
 * Obj.push (x);
 * int param_2 = Obj.pop ();
 * int param_3 = Obj.peek ();
 * BOOL Param_4 = Obj.empty (); */


can refer to: http://www.cnblogs.com/wanghui9072229/archive/2011/11/22/2259391.html

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.