Skip to content

Commit 62f58c8

Browse files
committed
feat: add solutions to lcof problem: No.31
面试题31. 栈的压入、弹出序列
1 parent ab00cfd commit 62f58c8

File tree

3 files changed

+62
-0
lines changed

3 files changed

+62
-0
lines changed

lcof/面试题31. 栈的压入、弹出序列/README.md

+36
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,42 @@ public:
124124
};
125125
```
126126
127+
### **TypeScript**
128+
129+
```ts
130+
function validateStackSequences(pushed: number[], popped: number[]): boolean {
131+
const stack = [];
132+
let i = 0;
133+
for (const num of pushed) {
134+
stack.push(num);
135+
while (stack.length !== 0 && stack[stack.length - 1] === popped[i]) {
136+
stack.pop();
137+
i++;
138+
}
139+
}
140+
return stack.length === 0;
141+
}
142+
```
143+
144+
### **Rust**
145+
146+
```rust
147+
impl Solution {
148+
pub fn validate_stack_sequences(pushed: Vec<i32>, popped: Vec<i32>) -> bool {
149+
let mut stack = Vec::new();
150+
let mut i = 0;
151+
for &num in pushed.iter() {
152+
stack.push(num);
153+
while !stack.is_empty() && *stack.last().unwrap() == popped[i] {
154+
stack.pop();
155+
i += 1;
156+
}
157+
}
158+
stack.len() == 0
159+
}
160+
}
161+
```
162+
127163
### **...**
128164

129165
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
impl Solution {
2+
pub fn validate_stack_sequences(pushed: Vec<i32>, popped: Vec<i32>) -> bool {
3+
let mut stack = Vec::new();
4+
let mut i = 0;
5+
for &num in pushed.iter() {
6+
stack.push(num);
7+
while !stack.is_empty() && *stack.last().unwrap() == popped[i] {
8+
stack.pop();
9+
i += 1;
10+
}
11+
}
12+
stack.len() == 0
13+
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
function validateStackSequences(pushed: number[], popped: number[]): boolean {
2+
const stack = [];
3+
let i = 0;
4+
for (const num of pushed) {
5+
stack.push(num);
6+
while (stack.length !== 0 && stack[stack.length - 1] === popped[i]) {
7+
stack.pop();
8+
i++;
9+
}
10+
}
11+
return stack.length === 0;
12+
}

0 commit comments

Comments
 (0)