@@ -58,7 +58,11 @@ pop() -> 5, pop() -> 3, pop() -> 2, pop() -> 1
58
58
59
59
<!-- solution:start -->
60
60
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.
62
66
63
67
<!-- tabs:start -->
64
68
@@ -67,13 +71,14 @@ pop() -> 5, pop() -> 3, pop() -> 2, pop() -> 1
67
71
``` python
68
72
class Solution :
69
73
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]:
74
79
stk.pop()
75
- j += 1
76
- return j == len (pushed )
80
+ i += 1
81
+ return i == len (popped )
77
82
```
78
83
79
84
#### Java
@@ -82,15 +87,15 @@ class Solution:
82
87
class Solution {
83
88
public boolean validateStackSequences (int [] pushed , int [] popped ) {
84
89
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 ]) {
89
94
stk. pop();
90
- ++ j ;
95
+ ++ i ;
91
96
}
92
97
}
93
- return j == pushed . length;
98
+ return i == popped . length;
94
99
}
95
100
}
96
101
```
@@ -102,15 +107,15 @@ class Solution {
102
107
public:
103
108
bool validateStackSequences(vector<int >& pushed, vector<int >& popped) {
104
109
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 ] ) {
109
114
stk.pop();
110
- ++j ;
115
+ ++i ;
111
116
}
112
117
}
113
- return j == pushed .size();
118
+ return i == popped .size();
114
119
}
115
120
};
116
121
```
@@ -120,32 +125,32 @@ public:
120
125
```go
121
126
func validateStackSequences(pushed []int, popped []int) bool {
122
127
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 ] {
127
132
stk = stk[:len(stk)-1]
128
- j ++
133
+ i ++
129
134
}
130
135
}
131
- return j == len(pushed )
136
+ return i == len(popped )
132
137
}
133
138
```
134
139
135
140
#### TypeScript
136
141
137
142
``` ts
138
143
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 ]) {
144
149
stk .pop ();
145
- ++ j ;
150
+ i ++ ;
146
151
}
147
152
}
148
- return j == pushed .length ;
153
+ return i === popped .length ;
149
154
}
150
155
```
151
156
@@ -154,16 +159,16 @@ function validateStackSequences(pushed: number[], popped: number[]): boolean {
154
159
``` rust
155
160
impl Solution {
156
161
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 ();
158
163
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 ();
163
168
i += 1 ;
164
169
}
165
170
}
166
- stack . len () == 0
171
+ i == popped . len ()
167
172
}
168
173
}
169
174
```
@@ -177,16 +182,16 @@ impl Solution {
177
182
* @return {boolean}
178
183
*/
179
184
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 ]) {
185
190
stk .pop ();
186
- ++ j ;
191
+ i ++ ;
187
192
}
188
193
}
189
- return j == pushed .length ;
194
+ return i === popped .length ;
190
195
};
191
196
```
192
197
@@ -196,16 +201,17 @@ var validateStackSequences = function (pushed, popped) {
196
201
public class Solution {
197
202
public bool ValidateStackSequences (int [] pushed , int [] popped ) {
198
203
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 ) {
202
207
stk .Push (x );
203
- while (stk .Count != 0 && stk .Peek () == popped [j ]) {
208
+ while (stk .Count > 0 && stk .Peek () == popped [i ]) {
204
209
stk .Pop ();
205
- ++ j ;
210
+ i ++ ;
206
211
}
207
212
}
208
- return stk .Count == 0 ;
213
+
214
+ return i == popped .Length ;
209
215
}
210
216
}
211
217
```
0 commit comments