利用栈模拟
class Solution {
public boolean isPopOrder(int [] pushV,int [] popV) {
if (pushV.length != popV.length) return false;
Deque<Integer> stack = new LinkedList<>();
int i = 0;
for (int val : pushV) {
stack.addLast(val);
while(!stack.isEmpty() && stack.peekLast() == popV[i]) {
stack.pollLast();
i++;
}
}
return stack.isEmpty();
}
}
时间复杂度比栈和margesort低
其实也是可以用树状数组or线段树求的