Sword refers to offer: stack of push, pop-up sequence

Source: Internet
Author: User
 Topic Description: Enter a sequence of two integers, and the first sequence represents the stack's indentation order, and determine whether the second sequence is the pop-up order of the stack. Suppose all the numbers on the stack are not equal. For example, the sequence 1,2,3,4,5 is the indentation sequence of a stack, the sequence 4,5,3,2,1 is a pop-up sequence corresponding to the stack sequence, but 4,3,5,1,2 cannot be the pop-up sequence of the pressure stack sequence .

This problem examines the stack in the STL, same as the train station problem on the Zoj.

Class Solution {Public:bool ispoporder (vector<int> pushv,vector<int> POPV) {int len1 = Pushv.si
        Ze (), len2 = Popv.size ();
        if (len1 = = 0 | | len2 = 0) return false;
            if (len1 = = 1 && len2 = 1) {if (pushv[0] = = Popv[0]) return true;
        else return false;
        } if (Len1!= len2) return false;
        Stack<int> s;
        int i = 0, j = 0;
            while (I < len1 && J < len1) {if (S.empty () | | pushv[i]!= POPV[J]) {
            S.push (Pushv[i]);
        i++;
                } if (pushv[i] = = Popv[j]) {i++;
                j + +;
                    while (!s.empty () && popv[j] = = S.top () && J < len1) {s.pop ();
                j + +;
}} if (j = = Len1)            return true;
    else return false; }
};

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.