剑指 Offer 31. 栈的压入、弹出序列
剑指 Offer 31. 栈的压入、弹出序列
输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否为该栈的弹出顺序。假设压入栈的所有数字均不相等。例如,序列 {1,2,3,4,5} 是某栈的压栈序列,序列 {4,5,3,2,1} 是该压栈序列对应的一个弹出序列,但 {4,3,5,1,2} 就不可能是该压栈序列的弹出序列。
示例 1:
输入:pushed = [1,2,3,4,5], popped = [4,5,3,2,1]
输出:true
解释:我们可以按以下顺序执行:
push(1), push(2), push(3), push(4), pop() -> 4,
push(5), pop() -> 5, pop() -> 3, pop() -> 2, pop() -> 1
示例 2:
输入:pushed = [1,2,3,4,5], popped = [4,3,5,1,2]
输出:false
解释:1 不能在 2 之前弹出。
提示:
- 0 <= pushed.length == popped.length <= 1000
- 0 <= pushed[i], popped[i] < 1000
- pushed 是 popped 的排列。
做题思路:
这道题主要是借助辅助栈弹出序列索引,然后和弹出的的popped相互比较,相等的话,则弹出序列,不相等则继续遍历,直到把所有辅助栈的序列弹出,即可输出true。但如果index==push.length的时候,就直接输出false。
class Solution {
Stack<Integer> stack = new Stack<>();
public boolean validateStackSequences(int[] pushed, int[] popped) {
int index = -1;
for (int i = 0; i < popped.length; i++) {
while(stack.isEmpty()||stack.peek()!=popped[i]){
index++;
if(index==pushed.length){
return false;
}
stack.push(pushed[index]);
}
stack.pop();
}
return true;
}
}
这是k神更为简洁的代码思路,强烈推荐好好学习借鉴下。
算法流程:
- 初始化: 辅助栈 stackstack ,弹出序列的索引 ii ;
- 遍历压栈序列: 各元素记为 numnum ;
- 元素 numnum 入栈;
- 循环出栈:若 stack 的栈顶元素 == 弹出序列元素 popped[i] ,则执行出栈与 i++;
- 返回值: 若 stack为空,则此弹出序列合法。
class Solution {
public boolean validateStackSequences(int[] pushed, int[] popped) {
Stack<Integer> stack = new Stack<>();
int i = 0;
for(int num : pushed) {
stack.push(num); // num 入栈
while(!stack.isEmpty() && stack.peek() == popped[i]) { // 循环判断与出栈
stack.pop();
i++;
}
}
return stack.isEmpty();
}
}
参考链接: