思路
解题
package swordPointingToTheOffer;
import java.util.Stack;
public class ThirtyOne {
/**
*
* @param pushed 入栈的序列
* @param popped 出栈的序列
* @return
*/
public static boolean validateStackSequences(int[] pushed, int[] popped) {
if(pushed ==null||pushed.length<=0){
return true;
}
int k = 0;
Stack<Integer> stack = new Stack<>();
for (int i = 0;i<pushed.length;i++){
stack.push(pushed[i]);
while (!stack.isEmpty()&&stack.peek().intValue()==popped[k]){
stack.pop();
k++;
}
}
return stack.isEmpty();
}
public static void main(String[] args) {
int[] pushed = {1,2,3,4,5};
int[]popped = {4,3,5,1,2};
int[]popped2 = {4,5,3,2,1};
System.out.println(validateStackSequences(pushed,popped2));
}
}