topic link
Niuke online oj question - stack push, pop sequence
topic description
Input two integer sequences, the first sequence represents the push order of the stack, please judge whether the second sequence may be the pop order of the stack. Assume that all numbers pushed onto the stack are unequal. For example, the sequence 1, 2, 3, 4, 5 is the push sequence of a certain stack, and the sequence 4, 5, 3, 2, 1 is a pop sequence corresponding to the push sequence, but 4, 3, 5, 1, 2 It cannot be the pop sequence of the push sequence.
- 0<=pushV.length == popV.length <=1000
- -1000<=pushV[i]<=1000
- All numbers of pushV are different
Topic example
Example 1
Input:
[1,2,3,4,5],[4,5,3,2,1]
return value:
true
Description:
You can pass push(1)=>push(2)=>push(3)=>push(4)=>pop()=>push(5)=>pop()=>pop()=>pop ()=>pop()
to get the sequence [4,5,3,2,1] and return true
Example 2
Input:
[1,2,3,4,5],[4,3,5,1,2]
return value:
false
Explanation:
Since it is the push sequence of [1,2,3,4,5] and the pop sequence of [4,3,5,1,2], it is required that 4, 3, 5 must be pushed in before 1, 2, And 1 and 2 cannot be popped up, but in such a pressing order, 1 cannot be popped up before 2, so if it cannot be formed, return false
problem solving ideas
First of all, if pushA or popA is empty, or the lengths of the two are different, it is definitely impossible to be the pop sequence of the stack, and return false directly
You can directly create a stack, define an i element to traverse the pushA sequence, and define a j element to traverse the popA sequence
First insert the pushA[i] element into the stack. If the stack is not empty and the value of the top element of the stack is equal to popA[j], then the top element of the stack will always be popped, j++
Repeat the above process, if the last stack is not empty, it means that there are still elements that have not been popped, and return false
For example:
first insert the push[i] element into the stack
, then pop[j] != stack.peek(), I++, continue to push arr[i] onto the stack,
then pop[j] != stack.peek(), I++, continue to push arr[i] onto the stack
. When i is 3, the element pushed into the stack is 4 at this time, pop[j] == stack.peek(), pop the top element of the stack, j++ at
this time pop[j] ! = stack.peek(), I++, continue to push to the top of the stack
At this time, pop[j] == stack.peek(), pop the top element of the stack, j++, repeat the above operations until the stack is empty, and
finally when i traverses the entire When pushingA array, the stack is empty, indicating that popA is the correct popup sequence, and returns true
full code
import java.util.*;
public class Solution {
public boolean IsPopOrder(int [] pushA,int [] popA) {
if(pushA == null || popA == null || pushA.length != popA.length){
return false;
}
Stack<Integer> stack = new Stack<>();
int j = 0;
for (int i = 0; i < pushA.length; i++){
stack.push(pushA[i]);
while(!stack.isEmpty() && stack.peek() == popA[j]){
stack.pop();
j++;
}
}
return stack.isEmpty();
}
}