-
topic:
Two input sequence of integers, the first sequence representing a pressed stack order, determines whether it is possible for the second sequence the order of the pop-up stack.
Such as: Assuming that all of the numbers are not equal pushed onto the stack. Such as a sequence of a sequence of 1,2,3,4,5 is pressed into the stack, the push sequence is 4,5,3,2,1 sequence corresponding to a pop-up.
4,3,5,1,2 but it is impossible to push pop-up sequence of the sequence. (Note: the length of the two sequences are equal)
-
Problem-solving ideas:
Build a temporary stack, the input sequence PUSHA [] data sequentially into stack pressure, into each of a number of related popA Comparative [] in the first position,
If yes, the pop-up, and then continue to press-fitting, and [] the second comparand with POPA position, if the same, then the pop-up, the number of a third position of the lower press-fitted and POPA number [] comparison,
Sequentially proceed, if in the end, the temporary number stack can pop up, then, the POPA [] is PUSHA [] of a pop-up sequence, described in the text relatively empty, the following list details the results of each step:
With stack Stack <Integer> temp = new Stack <Integer> (); pop-up sequence index int popIndex = 0;
Push [] = {1,2,3,4,5}; POPA [] = {4,5,3,2,1};
(1) pressed into the popA [popIndex] Comparative, 1! = 4; not eject, continue pressed;
(2) 2 is pressed into, and popA [popIndex] Comparative, 2! = 4; not eject, continue pressed;
(3) 3 pressed into the popA [popIndex] Comparative, 3! = 4; not eject, continue pressed;
(4) 4 pressed, and popA [popIndex] Comparative, 4 == 4; 4 pop, after a shift popIndex, popIndex = popIndex + 1 = 1 ;
(5) 5 pressed, [popIndex] compared popA, 5 == 5; 5 pop, after a shift popIndex, popIndex popIndex + = = 2. 1; // number of five are sequentially into the stack
(6) 5 pop the top element 3 , 3 and popA [popIndex] Comparative, 3 == 3; pop 3, after a shift popIndex, popIndex = popIndex + 1 = 3 ;
(7) After 3 pop the top element 2 , 2 and popA [popIndex] Comparative, 2 == 2; pop 2, after a shift popIndex, popIndex = popIndex + 1 = 4 ;
(8) 2 pop the top element 1 , 1 is compared with popA [popIndex], 1 == 1 ; 1 pop, after a shift popIndex, popIndex = popIndex + 1 = 5 ;
So far, all the elements are temporary pop the stack, the stack is empty, it means that pupA [] is popA [] of a pop-up sequence;
If popA [] = {4,5,3,1,2}; the case:
(1) pressed into the popA [popIndex] Comparative, 1! = 4; not eject, continue pressed;
(2) 2 is pressed into, and popA [popIndex] Comparative, 2! = 4; not eject, continue pressed;
(3) 3 pressed into the popA [popIndex] Comparative, 3! = 4; not eject, continue pressed;
(4) 4 pressed, and popA [popIndex] Comparative, 4 == 4; 4 pop, after a shift popIndex, popIndex = popIndex + 1 = 1 ;
(5) 5 pressed, [popIndex] compared popA, 5 == 5; 5 pop, after a shift popIndex, popIndex popIndex + = = 2. 1; // number of five are sequentially into the stack
(6) 5 pop the top element 3 , 3 and popA [popIndex] Comparative, 3 == 3; pop 3, after a shift popIndex, popIndex = popIndex + 1 = 3 ;
(7) 3 pop the top element 2 , 2 and popA [popIndex] Comparative, 2 = 1;! 2 is not ejected; 2, have not been ejected, so that the stack has been in a state data, the POPA [] not PUSHA [] of the pop-up sequence;
-
Code implementation (Java):
/ ** * * / Package com.cherish.SwordRefersToOffer; Import the java.util.Stack; / ** * @author Acer * Title: * two input sequence of integers, the first sequence representing a sequence of stack of press-fitting, please determination the second possibility for the sequence order of the pop stack. * Assuming that all figures are not pushed onto the stack equal. Such as a sequence of a sequence of 1,2,3,4,5 is pressed into the stack, the push sequence is 4,5,3,2,1 sequence corresponding to a sequence of pop *, but 4,3,5,1, 2 can not be a pop-up sequence of the push sequence. (Note: two equal length sequences) * * * / public class test_31_ push and pop the stack sequence { / ** * @param args * / public static void main (String [] args) { / / the TODO method of automatically generated stubs int [] = {1,2,3,4,5 PUSHA}; Int [] = {4,5,3,2,1 POPA }; System.out.println (IsPopOrder (PUSHA, POPA)); } public static Boolean IsPopOrder ( int [] PUSHA, int [] POPA) { IF (pushA.length == 0 || popA.length == 0 ) { return to false ; } Stack <Integer> TEMP = new new Stack <Integer> (); // for identifying the position of the pop-up sequence int popIndex = 0 ; for ( int I = 0; I <pushA.length; I ++ ) { Temp.push (PUSHA [I]); // if the stack is not empty, the top element and the pop-up sequence equal to the while (! Temp.empty () && temp.peek () == POPA [popIndex]) { // out stack temp.pop (); // pop-up sequence back a popIndex ++ ; } } return temp.empty (); } }
operation result:
-
Code implementation (C #):
the using the System; the using the System.Collections.Generic; the using the System.Linq; namespace test_31_ push and pop the stack sequence { class Program { static void the Main ( String [] args) { int [] = {pushV . 1 , 2 , . 3 , . 4 , . 5 }; int [] = {popV . 4 , . 5 , . 3 , 2 , . 1 }; Console.WriteLine (IsPopOrder (pushV, popV)); Console.ReadLine (); } public static bool IsPopOrder(int[] pushV, int[] popV) { // write code here if (pushV.Length == 0|| popV.Length == 0) { return false; } var stack = new Stack<int>(); int popIndex = 0; for (int i = 0; i < pushV.Length; i++) { Stack.Push (pushV [I]); the while (stack.Count> 0 && stack.Peek () Equals (popV [popIndex]).) { stack.Pop (); // pop the top element popIndex ++; // after a shift popV subscript } } return Stack .count () == 0 ; } } }
Achieve results: