File tree Expand file tree Collapse file tree 2 files changed +50
-0
lines changed Expand file tree Collapse file tree 2 files changed +50
-0
lines changed Original file line number Diff line number Diff line change @@ -5,6 +5,7 @@ public class Solution {
5
5
public static void main (String [] args ) {
6
6
Solution s = new Solution ();
7
7
System .out .println (s .uniquePath (3 , 2 ));
8
+ System .out .println (s .uniquePath (3 , 3 ));
8
9
System .out .println (s .uniquePath (7 , 3 ));
9
10
}
10
11
Original file line number Diff line number Diff line change
1
+ package com .blankj .medium ._063 ;
2
+
3
+ public class Solution {
4
+
5
+ public static void main (String [] args ) {
6
+ Solution s = new Solution ();
7
+ int [][] matrix = new int [][]{
8
+ {0 , 0 , 0 },
9
+ {0 , 1 , 0 },
10
+ {0 , 0 , 0 }
11
+ };
12
+ System .out .println (s .uniquePath (matrix ));
13
+ matrix = new int [][]{
14
+ {0 , 0 , 0 },
15
+ {0 , 0 , 0 },
16
+ {0 , 0 , 0 }
17
+ };
18
+ System .out .println (s .uniquePath (matrix ));
19
+ }
20
+
21
+ public int uniquePath (int [][] matrix ) {
22
+ if (matrix == null || matrix .length == 0 ) {
23
+ return -1 ;
24
+ }
25
+
26
+ int m = matrix .length ;
27
+ int n = matrix [0 ].length ;
28
+
29
+ int [][] f = new int [m ][n ];
30
+ for (int i = 0 ; i < m ; i ++) {
31
+ f [i ][0 ] = 1 ;
32
+ }
33
+ for (int j = 0 ; j < n ; j ++) {
34
+ f [0 ][j ] = 1 ;
35
+ }
36
+ for (int i = 1 ; i < m ; i ++) {
37
+ for (int j = 1 ; j < n ; j ++) {
38
+ f [i ][j ] = 0 ;
39
+ if (matrix [i ][j - 1 ] == 0 ) {
40
+ f [i ][j ] += f [i ][j - 1 ];
41
+ }
42
+ if (matrix [i - 1 ][j ] == 0 ) {
43
+ f [i ][j ] += f [i - 1 ][j ];
44
+ }
45
+ }
46
+ }
47
+ return f [m - 1 ][n - 1 ];
48
+ }
49
+ }
You can’t perform that action at this time.
0 commit comments