这题使用模拟法,也就是我们脑子咋分辨是否能够作为弹出序列的?
就是看看照着弹出序列能不能弹完,其实我觉得不管是有没有重复值这种模拟的方法都能正确的,比如下面这种,老实说其实不在意到底哪个4先弹出来。
1 4 2 3 5 4
4 5 3 2 4 1
import java.util.*;
public class Solution {
public boolean IsPopOrder(int [] pushA,int [] popA) {
Stack<Integer> stack = new Stack<>();
int b = 0;
for(int a:pushA){
stack.push(a);
while(!stack.isEmpty()&&stack.peek()==popA[b]){
stack.pop();
b++;
}
}
return stack.isEmpty();
}
} 
京公网安备 11010502036488号