File tree Expand file tree Collapse file tree 4 files changed +179
-0
lines changed
solution/1700-1799/1706.Where Will the Ball Fall Expand file tree Collapse file tree 4 files changed +179
-0
lines changed Original file line number Diff line number Diff line change @@ -210,6 +210,69 @@ func findBall(grid [][]int) []int {
210
210
}
211
211
```
212
212
213
+ ### ** TypeScript**
214
+
215
+ ``` ts
216
+ function findBall(grid : number [][]): number [] {
217
+ const m = grid .length ;
218
+ const n = grid [0 ].length ;
219
+ const res = new Array (n ).fill (0 );
220
+ const dfs = (i : number , j : number ) => {
221
+ if (i === m ) {
222
+ return j ;
223
+ }
224
+ if (grid [i ][j ] === 1 ) {
225
+ if (j === n - 1 || grid [i ][j + 1 ] === - 1 ) {
226
+ return - 1 ;
227
+ }
228
+ return dfs (i + 1 , j + 1 );
229
+ } else {
230
+ if (j === 0 || grid [i ][j - 1 ] === 1 ) {
231
+ return - 1 ;
232
+ }
233
+ return dfs (i + 1 , j - 1 );
234
+ }
235
+ };
236
+ for (let i = 0 ; i < n ; i ++ ) {
237
+ res [i ] = dfs (0 , i );
238
+ }
239
+ return res ;
240
+ }
241
+ ```
242
+
243
+ ### ** Rust**
244
+
245
+ ``` rust
246
+ impl Solution {
247
+ fn dfs (grid : & Vec <Vec <i32 >>, i : usize , j : usize ) -> i32 {
248
+ if i == grid . len () {
249
+ return j as i32 ;
250
+ }
251
+ if grid [i ][j ] == 1 {
252
+ if j == grid [0 ]. len () - 1 || grid [i ][j + 1 ] == - 1 {
253
+ return - 1 ;
254
+ }
255
+ Self :: dfs (grid , i + 1 , j + 1 )
256
+ } else {
257
+ if j == 0 || grid [i ][j - 1 ] == 1 {
258
+ return - 1 ;
259
+ }
260
+ Self :: dfs (grid , i + 1 , j - 1 )
261
+ }
262
+ }
263
+
264
+ pub fn find_ball (grid : Vec <Vec <i32 >>) -> Vec <i32 > {
265
+ let m = grid . len ();
266
+ let n = grid [0 ]. len ();
267
+ let mut res = vec! [0 ; n ];
268
+ for i in 0 .. n {
269
+ res [i ] = Self :: dfs (& grid , 0 , i );
270
+ }
271
+ res
272
+ }
273
+ }
274
+ ```
275
+
213
276
### ** ...**
214
277
215
278
```
Original file line number Diff line number Diff line change @@ -191,6 +191,69 @@ func findBall(grid [][]int) []int {
191
191
}
192
192
```
193
193
194
+ ### ** TypeScript**
195
+
196
+ ``` ts
197
+ function findBall(grid : number [][]): number [] {
198
+ const m = grid .length ;
199
+ const n = grid [0 ].length ;
200
+ const res = new Array (n ).fill (0 );
201
+ const dfs = (i : number , j : number ) => {
202
+ if (i === m ) {
203
+ return j ;
204
+ }
205
+ if (grid [i ][j ] === 1 ) {
206
+ if (j === n - 1 || grid [i ][j + 1 ] === - 1 ) {
207
+ return - 1 ;
208
+ }
209
+ return dfs (i + 1 , j + 1 );
210
+ } else {
211
+ if (j === 0 || grid [i ][j - 1 ] === 1 ) {
212
+ return - 1 ;
213
+ }
214
+ return dfs (i + 1 , j - 1 );
215
+ }
216
+ };
217
+ for (let i = 0 ; i < n ; i ++ ) {
218
+ res [i ] = dfs (0 , i );
219
+ }
220
+ return res ;
221
+ }
222
+ ```
223
+
224
+ ### ** Rust**
225
+
226
+ ``` rust
227
+ impl Solution {
228
+ fn dfs (grid : & Vec <Vec <i32 >>, i : usize , j : usize ) -> i32 {
229
+ if i == grid . len () {
230
+ return j as i32 ;
231
+ }
232
+ if grid [i ][j ] == 1 {
233
+ if j == grid [0 ]. len () - 1 || grid [i ][j + 1 ] == - 1 {
234
+ return - 1 ;
235
+ }
236
+ Self :: dfs (grid , i + 1 , j + 1 )
237
+ } else {
238
+ if j == 0 || grid [i ][j - 1 ] == 1 {
239
+ return - 1 ;
240
+ }
241
+ Self :: dfs (grid , i + 1 , j - 1 )
242
+ }
243
+ }
244
+
245
+ pub fn find_ball (grid : Vec <Vec <i32 >>) -> Vec <i32 > {
246
+ let m = grid . len ();
247
+ let n = grid [0 ]. len ();
248
+ let mut res = vec! [0 ; n ];
249
+ for i in 0 .. n {
250
+ res [i ] = Self :: dfs (& grid , 0 , i );
251
+ }
252
+ res
253
+ }
254
+ }
255
+ ```
256
+
194
257
### ** ...**
195
258
196
259
```
Original file line number Diff line number Diff line change
1
+ impl Solution {
2
+ fn dfs ( grid : & Vec < Vec < i32 > > , i : usize , j : usize ) -> i32 {
3
+ if i == grid. len ( ) {
4
+ return j as i32 ;
5
+ }
6
+ if grid[ i] [ j] == 1 {
7
+ if j == grid[ 0 ] . len ( ) - 1 || grid[ i] [ j + 1 ] == -1 {
8
+ return -1 ;
9
+ }
10
+ Self :: dfs ( grid, i + 1 , j + 1 )
11
+ } else {
12
+ if j == 0 || grid[ i] [ j - 1 ] == 1 {
13
+ return -1 ;
14
+ }
15
+ Self :: dfs ( grid, i + 1 , j - 1 )
16
+ }
17
+ }
18
+
19
+ pub fn find_ball ( grid : Vec < Vec < i32 > > ) -> Vec < i32 > {
20
+ let m = grid. len ( ) ;
21
+ let n = grid[ 0 ] . len ( ) ;
22
+ let mut res = vec ! [ 0 ; n] ;
23
+ for i in 0 ..n {
24
+ res[ i] = Self :: dfs ( & grid, 0 , i) ;
25
+ }
26
+ res
27
+ }
28
+ }
Original file line number Diff line number Diff line change
1
+ function findBall ( grid : number [ ] [ ] ) : number [ ] {
2
+ const m = grid . length ;
3
+ const n = grid [ 0 ] . length ;
4
+ const res = new Array ( n ) . fill ( 0 ) ;
5
+ const dfs = ( i : number , j : number ) => {
6
+ if ( i === m ) {
7
+ return j ;
8
+ }
9
+ if ( grid [ i ] [ j ] === 1 ) {
10
+ if ( j === n - 1 || grid [ i ] [ j + 1 ] === - 1 ) {
11
+ return - 1 ;
12
+ }
13
+ return dfs ( i + 1 , j + 1 ) ;
14
+ } else {
15
+ if ( j === 0 || grid [ i ] [ j - 1 ] === 1 ) {
16
+ return - 1 ;
17
+ }
18
+ return dfs ( i + 1 , j - 1 ) ;
19
+ }
20
+ } ;
21
+ for ( let i = 0 ; i < n ; i ++ ) {
22
+ res [ i ] = dfs ( 0 , i ) ;
23
+ }
24
+ return res ;
25
+ }
You can’t perform that action at this time.
0 commit comments