Question 29th: Determine if a sequence is a pop sequence of another push sequence

Source: Internet
Author: User

Github:https://github.com/frank-cq/mytest

Question 29th: Enter a sequence of two integers, in which a sequence 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 sequence, because can have the following push and pop sequence: Push 1, Push 2, push 3, Push 4,pop, push 5, Pop, pop, p OP, Pop. The resulting pop sequence is 4,5,3,2,1. However, sequence 4,3,5,1,2 cannot be a pop sequence of push sequences 1, 2, 3, 4, 5.


Code

 Packagetest029;ImportJava.util.Stack;/** * Created by CQ on 2015/6/28. * Question 29th: Enter two integer sequences, one of which represents the push order of the stack, and whether another sequence is likely to be the corresponding pop order. * For 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 sequence, because can have the following push and pop sequence: Push 1, Push 2, Push 3, Push 4, * pop, Push 5, pop, pop, pop, pop. The resulting pop sequence is 4,5,3,2,1. But the sequence 4,3,5,1,2 cannot be a sequence of pop sequences of the push sequence * 1, 2, 3, 4, 5. */ Public  class Test029 {     Public Static Boolean Iscorrespondpoporder(int[] Pusharr,int[] Poparr) {if(Pusharr.length! = poparr.length) {return false; } stack<integer> Stack =NewStack<integer> ();inti =0, j =0; while(I < Poparr.length) {//into the stack             while(J < Pusharr.length)                {Stack.push (pusharr[j]); j + +;if(pusharr[j-1] = = Poparr[i]) { Break; }            }//out of Stack            inttop = Stack.pop ();//Get top of stack element             while(I < Poparr.length) {//stack top element equals the current element of the stack sequence, then proceeds out of the stack                if(top = = Poparr[i]) {i++;if(Stack.isempty ()) {//Stack is empty, jump out of loop                         Break;                } top = Stack.pop (); }//stack top element is not equal to the current element of the stack sequence, restoring the top element of the stack                Else{Stack.push (top); Break; }            }///correctly, the stack is complete, and the stack is finished, because the stack            if(j = = Pusharr.length) { Break; }        }returnStack.isempty (); } Public Static void Main(string[] args) {int[] Pusharr = {1,2,3,4,5};int[] Poparr = {4,5,3,2,1};int[] popArr2 = {4,3,5,1,2}; System.out.println ("4,5,3,2,1 is an out-of-stack sequence for 1,2,3,4,5:"+iscorrespondpoporder (Pusharr,poparr)); System.out.println ("4,3,5,1,2 is an out-of-stack sequence for 1,2,3,4,5:"+iscorrespondpoporder (PUSHARR,POPARR2)); }}




Execution results

Connected to  theTarget VM, Address:' 127.0.0.1:25254 ', Transport:' socket 'Disconnected from  theTarget VM, Address:' 127.0.0.1:25254 ', Transport:' socket '4,5,3,2,1Is1,2,3,4,5An out-of-stack sequence:true4,3,5,1,2Is1,2,3,4,5An out-of-stack sequence:falseProcess finished withExit code0

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Question 29th: Determine if a sequence is a pop sequence of another push 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.