Stack push-in, pop-up sequence

Source: Internet
Author: User

Title Description
Enter a sequence of two integers, and the first sequence represents the stacking order of the stack, judging whether the second sequence is the pop-up order for the stack. Assume that all the numbers that are pressed into the stack are not equal. For example, the sequence 1,2,3,4,5 is the indentation order of a stack, and the sequence 4,5,3,2,1 is a pop-up sequence corresponding to the stack sequence, but 4,3,5,1,2 is not likely to be the pop-up sequence of the stack sequence.

To get this problem, the first thought is to put all the impossible out of the stack sequence to find out, and then look for and out of the stack sequence between the rules, but for a long time not found.

Look at the book of ideas, this is not your own brain when the idea of judgment?

into the stack sequence: 1, 2, 3, 4, 5
Out-of-stack sequence: 4, 5, 3, 2, 1

See the first out of the stack is 4, so before 4 must first into the stack, in the stack sequence to find this first out of the stack 4, by the way to customize a stack stack, let 1,2,3,4 into the stack in turn.
Stack this value is {1,2,3,4,}

Next, define a pointer popindex to the first element of the occurrence sequence, simulating the stack.
Stack.pop once, the pointer popindex moves backwards.
At this point, the stack top and the stack sequence pointers to values are not equal (3!). = 5)

At this point in the stack sequence to look for 5, if not found, then it indicates that this sequence is not a stack sequence.
If found, will this 5 into the stack, at this time stack={1,2,3,5};
The pointer popindex points to 5 in the stack sequence, then out of the stack once, Popindex forward, knowing that the popindex is greater than the length of the sequence.

The code is as follows:

public static Boolean Ispop (StringPush, StringPop{Boolean ISP = false;if(Push==null | |Pop==null)returnIspif(Push.length() !=Pop.length() )returnIspif(Pop.length() <=0)returnIsp Stack<integer> st = new Stack<integer> ();intpopindex=0, pushindex=0; while(Popindex <Pop.length())          { for(;p ushindex<Push.length();p ushindex++) {inttemp =Push. CharAt (Pushindex)-' 0 '; St.Push(temp);if(Pop. charAt (Popindex) = =Push. CharAt (Pushindex)) Break; A value in the stack was not found in the queueif(Pushindex >=Push.length()) Break; while(!st.empty () &&st.peek () = =Pop. CharAt (Popindex)-' 0 ') {St.Pop();            popindex++; }if(St.empty () &&Pop.length() ==popindex) isp=true;        pushindex++; }returnIsp }

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

Stack push-in, 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.