File tree 3 files changed +73
-0
lines changed
solution/0700-0799/0797.All Paths From Source to Target
3 files changed +73
-0
lines changed Original file line number Diff line number Diff line change @@ -223,6 +223,32 @@ var allPathsSourceTarget = function (graph) {
223
223
};
224
224
```
225
225
226
+ #### TypeScript
227
+
228
+ ``` ts
229
+ function allPathsSourceTarget(graph : number [][]): number [][] {
230
+ const ans: number [][] = [];
231
+
232
+ const dfs = (path : number []) => {
233
+ const curr = path .at (- 1 )! ;
234
+ if (curr === graph .length - 1 ) {
235
+ ans .push ([... path ]);
236
+ return ;
237
+ }
238
+
239
+ for (const v of graph [curr ]) {
240
+ path .push (v );
241
+ dfs (path );
242
+ path .pop ();
243
+ }
244
+ };
245
+
246
+ dfs ([0 ]);
247
+
248
+ return ans ;
249
+ }
250
+ ```
251
+
226
252
<!-- tabs:end -->
227
253
228
254
<!-- solution:end -->
Original file line number Diff line number Diff line change @@ -215,6 +215,32 @@ var allPathsSourceTarget = function (graph) {
215
215
};
216
216
```
217
217
218
+ #### TypeScript
219
+
220
+ ``` ts
221
+ function allPathsSourceTarget(graph : number [][]): number [][] {
222
+ const ans: number [][] = [];
223
+
224
+ const dfs = (path : number []) => {
225
+ const curr = path .at (- 1 )! ;
226
+ if (curr === graph .length - 1 ) {
227
+ ans .push ([... path ]);
228
+ return ;
229
+ }
230
+
231
+ for (const v of graph [curr ]) {
232
+ path .push (v );
233
+ dfs (path );
234
+ path .pop ();
235
+ }
236
+ };
237
+
238
+ dfs ([0 ]);
239
+
240
+ return ans ;
241
+ }
242
+ ```
243
+
218
244
<!-- tabs:end -->
219
245
220
246
<!-- solution:end -->
Original file line number Diff line number Diff line change
1
+ function allPathsSourceTarget ( graph : number [ ] [ ] ) : number [ ] [ ] {
2
+ const ans : number [ ] [ ] = [ ] ;
3
+
4
+ const dfs = ( path : number [ ] ) => {
5
+ const curr = path . at ( - 1 ) ! ;
6
+ if ( curr === graph . length - 1 ) {
7
+ ans . push ( [ ...path ] ) ;
8
+ return ;
9
+ }
10
+
11
+ for ( const v of graph [ curr ] ) {
12
+ path . push ( v ) ;
13
+ dfs ( path ) ;
14
+ path . pop ( ) ;
15
+ }
16
+ } ;
17
+
18
+ dfs ( [ 0 ] ) ;
19
+
20
+ return ans ;
21
+ }
You can’t perform that action at this time.
0 commit comments