import java.util.Stack;
public class Solution {
public boolean IsPopOrder(int [] pushA,int [] popA) {
if(pushA.length==0||popA.length==0)
{
return false;
}
Stack<Integer>stack=new Stack<>(); //开启的辅助栈
int popIndex=0;
for(int i=0;i<pushA.length;i++)
{
stack.push(pushA[i]); //压入辅助栈
while(!stack.isEmpty()&&stack.peek()==popA[popIndex])
{
stack.pop();
++popIndex;
}
}
return stack.isEmpty();
}
public static void main(String[]args){
int[]pushA={1,2,3,4,5};
int[]pushA2={1};
int[]popA={4,5,3,2,1};
int[]popA2={4,3,5,1,2};
Solution s=new Solution();
System.out.println(s.IsPopOrder(pushA,popA));
System.out.println(s.IsPopOrder(pushA,popA2));
}
}
栈的压入、弹出序列
猜你喜欢
转载自blog.csdn.net/u012017783/article/details/80203686
今日推荐
周排行