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. All figures are not pushed onto the stack is assumed 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 it is impossible to push pop-up sequence of the sequence. (Note: the length of the two sequences are equal)
import java.util.Stack;
public class Solution {
public boolean IsPopOrder(int [] pushA,int [] popA) {
if(pushA == null || popA == null || pushA.length == 0 || popA.length == 0){
return false;
}
Stack<Integer> stack = new Stack<>();
int i = 0;
for(int num: pushA){
stack.push(num);
while(!stack.isEmpty() && stack.peek() == popA[i]){
stack.pop();
i++;
}
}
return stack.isEmpty();
}
}