Determine if the stacking order is correct (stack push, pop-up sequence)

Source: Internet
Author: User

Enter a sequence of two integers. One of the sequences represents the push order of the stack, judging whether another sequence is likely to be the corresponding pop order. For the sake of simplicity, we assume that any two integers of the push sequence are not equal.

For example, the input push sequence is 1, 2, 3, 4, 5, then 4, 5, 3, 2, 1 may be a pop series. Because you can have the following push and pop sequence: Push 1,push 2,push 3,push 4,pop,push 5,pop,pop,pop,pop, so the resulting pop sequence is 4, 5, 3, 2, 1. But sequences 4, 3, 5, 1, 2 are not likely to be the sequence of pop sequences of the push sequence 1, 2, 3, 4, 5.

Directly on the code, there are two ways.

 Packagetest;ImportJava.util.Stack; Public classStackpopseq {/*** According to the number arrangement characteristics * if A[I+1] > A[i], then a[i+1] is greater than the previous number * that is, if the number of small to large out, must be in the out, then larger than their number, it is impossible to come out earlier than them *@paramA *@return     */    Private Static BooleanIspopseqbyno (int[] a) {BooleanFlag =true; intmax = A[0];  for(inti=1;i<a.length;i++){                if(a[i-1]<a[i]&&a[i]<max) {Flag=false;  Break; } Max= Math.max (Math.max (a[i-1],a[i]), Max); }            returnFlag;}/*** Through the simulation stack in and out to judge, * first for the loop to the ARR1 elements in the stack, and on each traversal, retrieve the arr2 can be pop elements. * If the loop ends, and there are elements in the stack, the arr2 sequence is not a pop sequence. * @paramA1 *@paramA2 *@return */    Private Static BooleanIspopseqbysimulate (int[] A1,int[] A2) {        //TODO auto-generated Method StubStack S=NewStack ();  for(inti=0,j=0;i<a1.length;i++) {s.push (a1[i]);  while(!s.empty () &&a2[j]== (int) S.peek ()) {s.pop (); J++; }                    }                returnS.size () ==0; }         Public Static voidMain (String[]args) {int[]a1={1,2,3,4,5,6,7}; int[]a2={1,2,3,7,6,5,4}; int[]a3={1,2,3,7,5,6,4};        System.out.println (Ispopseqbysimulate (A1,A2));                System.out.println (Ispopseqbysimulate (A1,A3));        System.out.println (Ispopseqbyno (A2));            System.out.println (Ispopseqbyno (A3)); }    }

Determine if the stacking order is correct (stack push, pop-up sequence)

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.