Skip to content

Commit c9e9bc6

Browse files
authoredJul 20, 2024
feat: add solutions to lc problem: No.0946 (doocs#3294)
No.0946.Validate Stack Sequences
1 parent dfec18b commit c9e9bc6

File tree

10 files changed

+164
-156
lines changed

10 files changed

+164
-156
lines changed
 

‎solution/0900-0999/0946.Validate Stack Sequences/README.md

+54-54
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,9 @@ push(5), pop() -> 5, pop() -> 3, pop() -> 2, pop() -> 1
6060

6161
### 方法一:栈模拟
6262

63-
遍历 `pushed` 序列,将每个数 `v` 依次压入栈中,压入后检查这个数是不是 `popped` 序列中下一个要弹出的值,如果是就循环把栈顶元素弹出
63+
我们遍历 $\textit{pushed}$ 数组,对于当前遍历到的元素 $x$,我们将其压入栈 $\textit{stk}$ 中,然后判断栈顶元素是否和 $\textit{popped}$ 数组中下一个要弹出的元素相等,如果相等,我们就将栈顶元素弹出并将 $\textit{popped}$ 数组中下一个要弹出的元素的索引 $i$ 加一。最后,如果要弹出的元素都能按照 $\textit{popped}$ 数组的顺序弹出,返回 $\textit{true}$,否则返回 $\textit{false}$
6464

65-
遍历结束,如果 `popped` 序列已经到末尾,说明是一个合法的序列,否则不是。
66-
67-
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是 `pushed` 序列的长度。
65+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为 $\textit{pushed}$ 数组的长度。
6866

6967
<!-- tabs:start -->
7068

@@ -73,13 +71,14 @@ push(5), pop() -&gt; 5, pop() -&gt; 3, pop() -&gt; 2, pop() -&gt; 1
7371
```python
7472
class Solution:
7573
def validateStackSequences(self, pushed: List[int], popped: List[int]) -> bool:
76-
j, stk = 0, []
77-
for v in pushed:
78-
stk.append(v)
79-
while stk and stk[-1] == popped[j]:
74+
stk = []
75+
i = 0
76+
for x in pushed:
77+
stk.append(x)
78+
while stk and stk[-1] == popped[i]:
8079
stk.pop()
81-
j += 1
82-
return j == len(pushed)
80+
i += 1
81+
return i == len(popped)
8382
```
8483

8584
#### Java
@@ -88,15 +87,15 @@ class Solution:
8887
class Solution {
8988
public boolean validateStackSequences(int[] pushed, int[] popped) {
9089
Deque<Integer> stk = new ArrayDeque<>();
91-
int j = 0;
92-
for (int v : pushed) {
93-
stk.push(v);
94-
while (!stk.isEmpty() && stk.peek() == popped[j]) {
90+
int i = 0;
91+
for (int x : pushed) {
92+
stk.push(x);
93+
while (!stk.isEmpty() && stk.peek() == popped[i]) {
9594
stk.pop();
96-
++j;
95+
++i;
9796
}
9897
}
99-
return j == pushed.length;
98+
return i == popped.length;
10099
}
101100
}
102101
```
@@ -108,15 +107,15 @@ class Solution {
108107
public:
109108
bool validateStackSequences(vector<int>& pushed, vector<int>& popped) {
110109
stack<int> stk;
111-
int j = 0;
112-
for (int v : pushed) {
113-
stk.push(v);
114-
while (!stk.empty() && stk.top() == popped[j]) {
110+
int i = 0;
111+
for (int x : pushed) {
112+
stk.push(x);
113+
while (stk.size() && stk.top() == popped[i]) {
115114
stk.pop();
116-
++j;
115+
++i;
117116
}
118117
}
119-
return j == pushed.size();
118+
return i == popped.size();
120119
}
121120
};
122121
```
@@ -126,32 +125,32 @@ public:
126125
```go
127126
func validateStackSequences(pushed []int, popped []int) bool {
128127
stk := []int{}
129-
j := 0
130-
for _, v := range pushed {
131-
stk = append(stk, v)
132-
for len(stk) > 0 && stk[len(stk)-1] == popped[j] {
128+
i := 0
129+
for _, x := range pushed {
130+
stk = append(stk, x)
131+
for len(stk) > 0 && stk[len(stk)-1] == popped[i] {
133132
stk = stk[:len(stk)-1]
134-
j++
133+
i++
135134
}
136135
}
137-
return j == len(pushed)
136+
return i == len(popped)
138137
}
139138
```
140139

141140
#### TypeScript
142141

143142
```ts
144143
function validateStackSequences(pushed: number[], popped: number[]): boolean {
145-
const stk = [];
146-
let j = 0;
147-
for (const v of pushed) {
148-
stk.push(v);
149-
while (stk.length && stk[stk.length - 1] == popped[j]) {
144+
const stk: number[] = [];
145+
let i = 0;
146+
for (const x of pushed) {
147+
stk.push(x);
148+
while (stk.length && stk.at(-1)! === popped[i]) {
150149
stk.pop();
151-
++j;
150+
i++;
152151
}
153152
}
154-
return j == pushed.length;
153+
return i === popped.length;
155154
}
156155
```
157156

@@ -160,16 +159,16 @@ function validateStackSequences(pushed: number[], popped: number[]): boolean {
160159
```rust
161160
impl Solution {
162161
pub fn validate_stack_sequences(pushed: Vec<i32>, popped: Vec<i32>) -> bool {
163-
let mut stack = Vec::new();
162+
let mut stk: Vec<i32> = Vec::new();
164163
let mut i = 0;
165-
for &num in pushed.iter() {
166-
stack.push(num);
167-
while !stack.is_empty() && *stack.last().unwrap() == popped[i] {
168-
stack.pop();
164+
for &x in &pushed {
165+
stk.push(x);
166+
while !stk.is_empty() && *stk.last().unwrap() == popped[i] {
167+
stk.pop();
169168
i += 1;
170169
}
171170
}
172-
stack.len() == 0
171+
i == popped.len()
173172
}
174173
}
175174
```
@@ -183,16 +182,16 @@ impl Solution {
183182
* @return {boolean}
184183
*/
185184
var validateStackSequences = function (pushed, popped) {
186-
let stk = [];
187-
let j = 0;
188-
for (const v of pushed) {
189-
stk.push(v);
190-
while (stk.length && stk[stk.length - 1] == popped[j]) {
185+
const stk = [];
186+
let i = 0;
187+
for (const x of pushed) {
188+
stk.push(x);
189+
while (stk.length && stk.at(-1) === popped[i]) {
191190
stk.pop();
192-
++j;
191+
i++;
193192
}
194193
}
195-
return j == pushed.length;
194+
return i === popped.length;
196195
};
197196
```
198197

@@ -202,16 +201,17 @@ var validateStackSequences = function (pushed, popped) {
202201
public class Solution {
203202
public bool ValidateStackSequences(int[] pushed, int[] popped) {
204203
Stack<int> stk = new Stack<int>();
205-
int j = 0;
206-
foreach (int x in pushed)
207-
{
204+
int i = 0;
205+
206+
foreach (int x in pushed) {
208207
stk.Push(x);
209-
while (stk.Count != 0 && stk.Peek() == popped[j]) {
208+
while (stk.Count > 0 && stk.Peek() == popped[i]) {
210209
stk.Pop();
211-
++j;
210+
i++;
212211
}
213212
}
214-
return stk.Count == 0;
213+
214+
return i == popped.Length;
215215
}
216216
}
217217
```

‎solution/0900-0999/0946.Validate Stack Sequences/README_EN.md

+57-51
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,11 @@ pop() -&gt; 5, pop() -&gt; 3, pop() -&gt; 2, pop() -&gt; 1
5858

5959
<!-- solution:start -->
6060

61-
### Solution 1
61+
### Solution 1: Stack Simulation
62+
63+
We iterate through the $\textit{pushed}$ array. For the current element $x$ being iterated, we push it into the stack $\textit{stk}$. Then, we check if the top element of the stack is equal to the next element to be popped in the $\textit{popped}$ array. If they are equal, we pop the top element from the stack and increment the index $i$ of the next element to be popped in the $\textit{popped}$ array. Finally, if all elements can be popped in the order specified by the $\textit{popped}$ array, return $\textit{true}$; otherwise, return $\textit{false}$.
64+
65+
The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the $\textit{pushed}$ array.
6266

6367
<!-- tabs:start -->
6468

@@ -67,13 +71,14 @@ pop() -&gt; 5, pop() -&gt; 3, pop() -&gt; 2, pop() -&gt; 1
6771
```python
6872
class Solution:
6973
def validateStackSequences(self, pushed: List[int], popped: List[int]) -> bool:
70-
j, stk = 0, []
71-
for v in pushed:
72-
stk.append(v)
73-
while stk and stk[-1] == popped[j]:
74+
stk = []
75+
i = 0
76+
for x in pushed:
77+
stk.append(x)
78+
while stk and stk[-1] == popped[i]:
7479
stk.pop()
75-
j += 1
76-
return j == len(pushed)
80+
i += 1
81+
return i == len(popped)
7782
```
7883

7984
#### Java
@@ -82,15 +87,15 @@ class Solution:
8287
class Solution {
8388
public boolean validateStackSequences(int[] pushed, int[] popped) {
8489
Deque<Integer> stk = new ArrayDeque<>();
85-
int j = 0;
86-
for (int v : pushed) {
87-
stk.push(v);
88-
while (!stk.isEmpty() && stk.peek() == popped[j]) {
90+
int i = 0;
91+
for (int x : pushed) {
92+
stk.push(x);
93+
while (!stk.isEmpty() && stk.peek() == popped[i]) {
8994
stk.pop();
90-
++j;
95+
++i;
9196
}
9297
}
93-
return j == pushed.length;
98+
return i == popped.length;
9499
}
95100
}
96101
```
@@ -102,15 +107,15 @@ class Solution {
102107
public:
103108
bool validateStackSequences(vector<int>& pushed, vector<int>& popped) {
104109
stack<int> stk;
105-
int j = 0;
106-
for (int v : pushed) {
107-
stk.push(v);
108-
while (!stk.empty() && stk.top() == popped[j]) {
110+
int i = 0;
111+
for (int x : pushed) {
112+
stk.push(x);
113+
while (stk.size() && stk.top() == popped[i]) {
109114
stk.pop();
110-
++j;
115+
++i;
111116
}
112117
}
113-
return j == pushed.size();
118+
return i == popped.size();
114119
}
115120
};
116121
```
@@ -120,32 +125,32 @@ public:
120125
```go
121126
func validateStackSequences(pushed []int, popped []int) bool {
122127
stk := []int{}
123-
j := 0
124-
for _, v := range pushed {
125-
stk = append(stk, v)
126-
for len(stk) > 0 && stk[len(stk)-1] == popped[j] {
128+
i := 0
129+
for _, x := range pushed {
130+
stk = append(stk, x)
131+
for len(stk) > 0 && stk[len(stk)-1] == popped[i] {
127132
stk = stk[:len(stk)-1]
128-
j++
133+
i++
129134
}
130135
}
131-
return j == len(pushed)
136+
return i == len(popped)
132137
}
133138
```
134139

135140
#### TypeScript
136141

137142
```ts
138143
function validateStackSequences(pushed: number[], popped: number[]): boolean {
139-
const stk = [];
140-
let j = 0;
141-
for (const v of pushed) {
142-
stk.push(v);
143-
while (stk.length && stk[stk.length - 1] == popped[j]) {
144+
const stk: number[] = [];
145+
let i = 0;
146+
for (const x of pushed) {
147+
stk.push(x);
148+
while (stk.length && stk.at(-1)! === popped[i]) {
144149
stk.pop();
145-
++j;
150+
i++;
146151
}
147152
}
148-
return j == pushed.length;
153+
return i === popped.length;
149154
}
150155
```
151156

@@ -154,16 +159,16 @@ function validateStackSequences(pushed: number[], popped: number[]): boolean {
154159
```rust
155160
impl Solution {
156161
pub fn validate_stack_sequences(pushed: Vec<i32>, popped: Vec<i32>) -> bool {
157-
let mut stack = Vec::new();
162+
let mut stk: Vec<i32> = Vec::new();
158163
let mut i = 0;
159-
for &num in pushed.iter() {
160-
stack.push(num);
161-
while !stack.is_empty() && *stack.last().unwrap() == popped[i] {
162-
stack.pop();
164+
for &x in &pushed {
165+
stk.push(x);
166+
while !stk.is_empty() && *stk.last().unwrap() == popped[i] {
167+
stk.pop();
163168
i += 1;
164169
}
165170
}
166-
stack.len() == 0
171+
i == popped.len()
167172
}
168173
}
169174
```
@@ -177,16 +182,16 @@ impl Solution {
177182
* @return {boolean}
178183
*/
179184
var validateStackSequences = function (pushed, popped) {
180-
let stk = [];
181-
let j = 0;
182-
for (const v of pushed) {
183-
stk.push(v);
184-
while (stk.length && stk[stk.length - 1] == popped[j]) {
185+
const stk = [];
186+
let i = 0;
187+
for (const x of pushed) {
188+
stk.push(x);
189+
while (stk.length && stk.at(-1) === popped[i]) {
185190
stk.pop();
186-
++j;
191+
i++;
187192
}
188193
}
189-
return j == pushed.length;
194+
return i === popped.length;
190195
};
191196
```
192197

@@ -196,16 +201,17 @@ var validateStackSequences = function (pushed, popped) {
196201
public class Solution {
197202
public bool ValidateStackSequences(int[] pushed, int[] popped) {
198203
Stack<int> stk = new Stack<int>();
199-
int j = 0;
200-
foreach (int x in pushed)
201-
{
204+
int i = 0;
205+
206+
foreach (int x in pushed) {
202207
stk.Push(x);
203-
while (stk.Count != 0 && stk.Peek() == popped[j]) {
208+
while (stk.Count > 0 && stk.Peek() == popped[i]) {
204209
stk.Pop();
205-
++j;
210+
i++;
206211
}
207212
}
208-
return stk.Count == 0;
213+
214+
return i == popped.Length;
209215
}
210216
}
211217
```

0 commit comments

Comments
 (0)