赞
踩
代码实现:
import java.util.*; public class Solution { public boolean IsPopOrder(int [] pushA,int [] popA) { Stack<Integer> stack = new Stack<>(); int j = 0; int n = pushA.length; for(int i = 0;i < n;i++){ //入栈操作,当栈为空,或栈顶元素不为出栈数组 while(j < n && (stack.isEmpty()||stack.peek() != popA[i])){ stack.push(pushA[j]); j++; } //查看是否出栈 if(stack.peek() == popA[i]){ stack.pop(); } else{ return false; } } //全部完成后表明正确 return true; } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。