用一个辅助栈来解决问题
不要想的太复杂
class Solution {
public boolean validateStackSequences(int[] pushed, int[] popped) {
Stack<Integer> res = new Stack<>();
int i=0;
for(int num : pushed){
res.push(num);
while(!res.isEmpty()&&res.peek()==popped[i]){
res.pop();
i++;
}
}
return res.isEmpty();
}
}