File tree 4 files changed +128
-0
lines changed
solution/0000-0099/0020.Valid Parentheses
4 files changed +128
-0
lines changed Original file line number Diff line number Diff line change @@ -242,6 +242,52 @@ def is_valid(s)
242
242
end
243
243
```
244
244
245
+ ### ** TypeScript**
246
+
247
+ ``` ts
248
+ const map = new Map ([
249
+ [' (' , ' )' ],
250
+ [' [' , ' ]' ],
251
+ [' {' , ' }' ],
252
+ ]);
253
+
254
+ function isValid(s : string ): boolean {
255
+ const stack = [];
256
+ for (const c of s ) {
257
+ if (map .has (c )) {
258
+ stack .push (map .get (c ));
259
+ } else if (stack .pop () !== c ) {
260
+ return false ;
261
+ }
262
+ }
263
+ return stack .length === 0 ;
264
+ }
265
+ ```
266
+
267
+ ### ** Rust**
268
+
269
+ ``` rust
270
+ use std :: collections :: HashMap ;
271
+
272
+ impl Solution {
273
+ pub fn is_valid (s : String ) -> bool {
274
+ let mut map = HashMap :: new ();
275
+ map . insert ('(' , ')' );
276
+ map . insert ('[' , ']' );
277
+ map . insert ('{' , '}' );
278
+ let mut stack = vec! [];
279
+ for c in s . chars () {
280
+ if map . contains_key (& c ) {
281
+ stack . push (map [& c ]);
282
+ } else if stack . pop (). unwrap_or (' ' ) != c {
283
+ return false ;
284
+ }
285
+ }
286
+ stack . len () == 0
287
+ }
288
+ }
289
+ ```
290
+
245
291
### ** ...**
246
292
247
293
```
Original file line number Diff line number Diff line change @@ -227,6 +227,52 @@ def is_valid(s)
227
227
end
228
228
```
229
229
230
+ ### ** TypeScript**
231
+
232
+ ``` ts
233
+ const map = new Map ([
234
+ [' (' , ' )' ],
235
+ [' [' , ' ]' ],
236
+ [' {' , ' }' ],
237
+ ]);
238
+
239
+ function isValid(s : string ): boolean {
240
+ const stack = [];
241
+ for (const c of s ) {
242
+ if (map .has (c )) {
243
+ stack .push (map .get (c ));
244
+ } else if (stack .pop () !== c ) {
245
+ return false ;
246
+ }
247
+ }
248
+ return stack .length === 0 ;
249
+ }
250
+ ```
251
+
252
+ ### ** Rust**
253
+
254
+ ``` rust
255
+ use std :: collections :: HashMap ;
256
+
257
+ impl Solution {
258
+ pub fn is_valid (s : String ) -> bool {
259
+ let mut map = HashMap :: new ();
260
+ map . insert ('(' , ')' );
261
+ map . insert ('[' , ']' );
262
+ map . insert ('{' , '}' );
263
+ let mut stack = vec! [];
264
+ for c in s . chars () {
265
+ if map . contains_key (& c ) {
266
+ stack . push (map [& c ]);
267
+ } else if stack . pop (). unwrap_or (' ' ) != c {
268
+ return false ;
269
+ }
270
+ }
271
+ stack . len () == 0
272
+ }
273
+ }
274
+ ```
275
+
230
276
### ** ...**
231
277
232
278
```
Original file line number Diff line number Diff line change
1
+ use std:: collections:: HashMap ;
2
+
3
+ impl Solution {
4
+ pub fn is_valid ( s : String ) -> bool {
5
+ let mut map = HashMap :: new ( ) ;
6
+ map. insert ( '(' , ')' ) ;
7
+ map. insert ( '[' , ']' ) ;
8
+ map. insert ( '{' , '}' ) ;
9
+ let mut stack = vec ! [ ] ;
10
+ for c in s. chars ( ) {
11
+ if map. contains_key ( & c) {
12
+ stack. push ( map[ & c] ) ;
13
+ } else if stack. pop ( ) . unwrap_or ( ' ' ) != c {
14
+ return false ;
15
+ }
16
+ }
17
+ stack. len ( ) == 0
18
+ }
19
+ }
Original file line number Diff line number Diff line change
1
+ const map = new Map ( [
2
+ [ '(' , ')' ] ,
3
+ [ '[' , ']' ] ,
4
+ [ '{' , '}' ] ,
5
+ ] ) ;
6
+
7
+ function isValid ( s : string ) : boolean {
8
+ const stack = [ ] ;
9
+ for ( const c of s ) {
10
+ if ( map . has ( c ) ) {
11
+ stack . push ( map . get ( c ) ) ;
12
+ } else if ( stack . pop ( ) !== c ) {
13
+ return false ;
14
+ }
15
+ }
16
+ return stack . length === 0 ;
17
+ }
You can’t perform that action at this time.
0 commit comments