题目要求

思路一:双指针
- 两个指针分别指向当前应推入or弹出的元素,不断更新栈顶,检查遍历完两个序列时栈顶的位置。
- 此处直接把
pushed数组当成栈用了,若有要求可以重新定义一个数组用【即思路二】。
Java
class Solution {
public boolean validateStackSequences(int[] pushed, int[] popped) {
int idx = 0;
for (int i = 0, j = 0; i < pushed.length; i++) {
pushed[idx++] = pushed[i];
while (idx > 0 && pushed[idx - 1] == popped[j] && ++j >= 0)
idx--;
}
return idx == 0;
}
}
- 时间复杂度:O(n)
- 空间复杂度:O(1)
C++
class Solution {
public:
bool validateStackSequences(vector<int>& pushed, vector<int>& popped) {
int idx = 0;
for (int i = 0, j = 0; i < pushed.size(); i++) {
pushed[idx++] = pushed[i];
while (idx > 0 && pushed[idx - 1] == popped[j] && ++j >= 0)
idx--;
}
return idx == 0;
}
};
- 时间复杂度:O(n)
- 空间复杂度:O(1)
Rust
impl Solution {
pub fn validate_stack_sequences(mut pushed: Vec<i32>, popped: Vec<i32>) -> bool {
let mut idx = 0;
let mut j = 0;
for i in 0..pushed.len() {
pushed[idx] = pushed[i];
idx += 1;
while idx > 0 && pushed[idx - 1] == popped[j] {
idx -= 1;
j += 1;
}
}
idx == 0
}
}
- 时间复杂度:O(n)
- 空间复杂度:O(1)
思路二:栈模拟
- 定义一个栈,按照给出的序列模拟一遍,检查栈是否空即可。
Java
class Solution {
public boolean validateStackSequences(int[] pushed, int[] popped) {
Deque<Integer> sta = new ArrayDeque<>();
for (int i =0, j = 0; i < pushed.length; i++) {
sta.addLast(pushed[i]);
while(!sta.isEmpty() && sta.peekLast() == popped[j] && ++j >= 0)
sta.pollLast();
}
return sta.isEmpty();
}
}
- 时间复杂度:O(n)
- 空间复杂度:O(n)
C++
class Solution {
public:
bool validateStackSequences(vector<int>& pushed, vector<int>& popped) {
stack<int> sta;
for (int i = 0, j = 0; i < pushed.size(); i++) {
sta.emplace(pushed[i]);
while (!sta.empty() && sta.top() == popped[j] && ++j >= 0)
sta.pop();
}
return sta.empty();
}
};
- 时间复杂度:O(n)
- 空间复杂度:O(n)
Rust
for循环也可以用for_each,反正遍历一遍pushed就结束。
impl Solution {
pub fn validate_stack_sequences(mut pushed: Vec<i32>, popped: Vec<i32>) -> bool {
let mut sta = vec![];
let mut j = 0;
for i in 0..pushed.len() {
sta.push(pushed[i]);
while let Some(&top) = sta.last() {
if top != popped[j] {
break;
}
sta.pop();
j += 1;
}
}
sta.is_empty()
}
}
- 时间复杂度:O(n)
- 空间复杂度:O(n)
总结
是我最爱的双指针!一个很简单的数据结构应用题,回想起了刚学栈的青涩时光~