The sword refers to the offer surface question (Java edition): The Stack's press-in, the popup sequence

Source: Internet
Author: User

Title: Enter a sequence of two integers, the first sequence represents the stacking order of the stack, and determine if 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, sequence 1,2,3,4,5 is a stack of stacks of stack sequence, sequence, 4,5,3,2,1 is the stack sequence corresponding to a pop-up sequence, but 4,3,5,1,2 can not be the stack sequence of pop-up sequence.

The intuitive idea to solve this problem is to create a secondary stack that presses the numbers in the first sequence of input into the secondary stack sequentially, and then pops the number from the stack in the order of the second sequence.

Take the pop-up sequence 4,5,3,2,1 as an example to analyze the stack and eject process. The first number to be ejected is 4, so 4 needs to be pressed into the secondary stack first. The order in which the stacks are pressed is determined by the stack sequence, which means that before the 4 is pressed into the stack, the digital all-in-the-box must be pressed into the stack first. At this time, the stack contains 4 numbers, respectively, 1,2,3,4, of which 4 are at the top of the stack. After putting the 4 pop-up stack, the remaining three digits are three-to-one. The next number that you want to be popped is 5, because it's not the top of the stack, so we then press the number after the first sequence, CMB 4, into the secondary stack until the number 5 is pressed in. This time 5 is at the top of the stack. The next three numbers that you want to be ejected are 3,2,1. Because they are on top of the stack before each operation, they pop up directly. The following table describes the process of entering and out of the stack in this example.


Summing up the above into the stack, the process of the stack, we suspect to find that a sequence is not the stack of the sequence of the rule: if the next pop-up number is just the top number, then directly popped. If the next pop-up number is not at the top of the stack, we will press the number of the stack in the stack to the auxiliary stack, knowing that the next number to eject will be pushed to the top of the stack. If all the numbers are pressed into the stack and the next number is not found, then the sequence cannot be a pop-up sequence.

After the formation of clear ideas, we can have some code, the following is the implementation of Java code.

/** * Enter a sequence of two integers, the first sequence represents the stack's indentation order, and determine if the second sequence is the pop-up order for the stack. */package swordforoffer;import java.util.stack;/** * @author Jinshuangqi * * August 3, 2015 */public class E22stackpushpoporde R {public boolean check (int[] ppush,int[] ppop) {Boolean ispossible = False;if (Ppush! = NULL && Ppop! = null) {Stack <Integer> stack = new stack<integer> (), int i = 0,J = 0;//Check each out-of-stack while (J-<ppop.length) {while (I < Ppush.len Gth && ppop[j]!= Ppush[i]) {Stack.push (ppush[i]); i++;} The equal elements do not have a stack ++i;++j;int top = 0;//the stack comparison while ((!stack.empty ()) && (top = Stack.pop ()) = = Ppop[j]) {++j;} if (J < ppop.length) {Stack.push (top);} Exits if (i >=ppush.length &&!stack.empty ()) {break;}} when no input stack element has been found if (Stack.empty ()) {ispossible = true;}} return ispossible;}          public static void Main (string[] args) {//in-stack sequence int[] Ppush = {1, 2, 3, 4, 5};  Out stack sequence int[] Ppop = {4, 3, 5, 2, 1}; {6,4, 3, 5, 2, 1};//{4,3,5,1,2} boolean flag = new E22stackpushpoporder (). Check (Ppush, PPop);  SYSTEM.OUT.PRINTLN (flag); }}


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

The sword refers to the offer surface question (Java edition): The Stack's press-in, the popup 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.