题目描述
输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否为该栈的弹出顺序。假设压入栈的所有数字均不相等。例如,序列 {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
的排列。
注意:本题与主站 946 题相同:https://leetcode.cn/problems/validate-stack-sequences/
解法
方法一:栈模拟
遍历 pushed
序列,将每个数 v
依次压入栈中,压入后检查这个数是不是 popped
序列中下一个要弹出的值,如果是就循环把栈顶元素弹出。
遍历结束,如果 popped
序列已经到末尾,说明是一个合法的序列,否则不是。
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是 pushed
序列的长度。
| class Solution:
def validateStackSequences(self, pushed: List[int], popped: List[int]) -> bool:
j, stk = 0, []
for v in pushed:
stk.append(v)
while stk and stk[-1] == popped[j]:
stk.pop()
j += 1
return j == len(pushed)
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14 | class Solution {
public boolean validateStackSequences(int[] pushed, int[] popped) {
Deque<Integer> stk = new ArrayDeque<>();
int j = 0;
for (int v : pushed) {
stk.push(v);
while (!stk.isEmpty() && stk.peek() == popped[j]) {
stk.pop();
++j;
}
}
return j == pushed.length;
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 | class Solution {
public:
bool validateStackSequences(vector<int>& pushed, vector<int>& popped) {
stack<int> stk;
int j = 0;
for (int v : pushed) {
stk.push(v);
while (!stk.empty() && stk.top() == popped[j]) {
stk.pop();
++j;
}
}
return j == pushed.size();
}
};
|
1
2
3
4
5
6
7
8
9
10
11
12 | func validateStackSequences(pushed []int, popped []int) bool {
stk := []int{}
j := 0
for _, v := range pushed {
stk = append(stk, v)
for len(stk) > 0 && stk[len(stk)-1] == popped[j] {
stk = stk[:len(stk)-1]
j++
}
}
return j == len(pushed)
}
|
1
2
3
4
5
6
7
8
9
10
11
12 | function validateStackSequences(pushed: number[], popped: number[]): boolean {
const stk = [];
let j = 0;
for (const v of pushed) {
stk.push(v);
while (stk.length && stk[stk.length - 1] == popped[j]) {
stk.pop();
++j;
}
}
return j == pushed.length;
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14 | impl Solution {
pub fn validate_stack_sequences(pushed: Vec<i32>, popped: Vec<i32>) -> bool {
let mut stack = Vec::new();
let mut i = 0;
for &num in pushed.iter() {
stack.push(num);
while !stack.is_empty() && *stack.last().unwrap() == popped[i] {
stack.pop();
i += 1;
}
}
stack.len() == 0
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 | /**
* @param {number[]} pushed
* @param {number[]} popped
* @return {boolean}
*/
var validateStackSequences = function (pushed, popped) {
let stk = [];
let j = 0;
for (const v of pushed) {
stk.push(v);
while (stk.length && stk[stk.length - 1] == popped[j]) {
stk.pop();
++j;
}
}
return j == pushed.length;
};
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 | public class Solution {
public bool ValidateStackSequences(int[] pushed, int[] popped) {
Stack<int> stk = new Stack<int>();
int j = 0;
foreach (int x in pushed)
{
stk.Push(x);
while (stk.Count != 0 && stk.Peek() == popped[j]) {
stk.Pop();
++j;
}
}
return stk.Count == 0;
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 | class Solution {
func validateStackSequences(_ pushed: [Int], _ popped: [Int]) -> Bool {
var stack = [Int]()
var j = 0
for v in pushed {
stack.append(v)
while !stack.isEmpty && stack.last == popped[j] {
stack.removeLast()
j += 1
}
}
return j == pushed.count
}
}
|