【LeetCode】剑指 Offer 31. 栈的压入、弹出序列
![在这里插入图片描述](https://img-blog.csdnimg.cn/a4f67a480ccf429292d921c8e064bd16.png)
package offer;
import java.util.Stack;
public class Solution31 {
public static void main(String[] args) {
int[] pushed = {1,2,3,4,5};
int[] popped = {4,5,3,2,1};
Solution31 solution = new Solution31();
System.out.println(solution.method(pushed, popped));
}
private boolean method(int[] pushed, int[] popped){
int i = 0;
Stack<Integer> stack = new Stack<>();
for(int num : pushed){
stack.push(num);
while(!stack.isEmpty() && stack.peek() == popped[i]){
stack.pop();
i++;
}
}
return stack.isEmpty();
}
}