File tree 3 files changed +62
-0
lines changed
3 files changed +62
-0
lines changed Original file line number Diff line number Diff line change @@ -124,6 +124,42 @@ public:
124
124
};
125
125
```
126
126
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
+
127
163
### ** ...**
128
164
129
165
```
Original file line number Diff line number Diff line change
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 number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments