1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
| // @Title: 栈的压入、弹出序列 (栈的压入、弹出序列 LCOF)
// @Author: 15816537946@163.com
// @Date: 2022-02-27 18:05:04
// @Runtime: 4 ms
// @Memory: 3.6 MB
func validateStackSequences(pushed []int, popped []int) bool {
stack := make([]int, 0)
var idx int
for _,v :=range pushed {
stack = append(stack, v)
for len(stack) > 0 && stack[len(stack)-1] == popped[idx] {
stack = stack[:len(stack)-1] // poped the last element
idx++
}
}
return len(stack) == 0
}
|