A stack sequence and a stack sequence are given to determine if the stack sequence is a stack sequence corresponding to the stack sequence.

Source: Internet
Author: User

Note: In the process of the stack, not all elements are in the stack after the stack, it is possible to stack a few elements, out of one or more stacks, and then continue into the stack, into the stack sequence can only ensure that all elements in the specified sequence into the stack.

Idea: The use of a secondary stack and a pointer to the first element of the stack sequence (int count = 0;), the use of a loop at the same time traversing the stack sequence into the stack to determine whether the current stack element is not the current subscript point to the stack element, is to remove the top element of the stack, and then add one, continue into the stack Until the traversal of the stack sequence is completed. Then determine if count has exceeded the range of the stack sequence, if it has been exceeded and the secondary stack is not empty, it is not the corresponding out of the stack sequence, return Fasle, if Count still does not reach the last element of the stack sequence subscript, then from conut start traversing the entire stack sequence, Each traversal is compared to the top element of the auxiliary stack, if the same as the top element of the stack and Count plus one, until the end of the traversal, determine whether the secondary stack is empty, for the null proof is the correct stack sequence, returns True, otherwise false. The code is as follows:

Import java.util.ArrayList;
Import Java.util.Stack;

public class Solution {public
    boolean ispoporder (int [] pusha,int [] popA) {
        stack<integer> Stack = new Sta Ck<> ();
        
        int count = 0;
        for (int i:pusha) {
            stack.push (i);
            if (Stack.peek () ==popa[count]) {
                stack.pop ();
                count++;
            }
        }
        while (!stack.isempty () &&count<popa.length) {
            if (Stack.peek () ==popa[count]) {
                stack.pop ();
            }
            count++;
        }
        
        return Stack.isempty ();  
    }
}


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.