-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.java
More file actions
24 lines (22 loc) · 750 Bytes
/
Copy pathSolution.java
File metadata and controls
24 lines (22 loc) · 750 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package leetcode._62_;
class Solution {
public int uniquePaths(int m, int n) {
int[][] ways = new int[m + 1][n + 1]; // 所有的格子都能到达,因此初始化为0,表示没有计算过。该数组用于保存到达该坐标的路径个数,防止重复计算
return uniquePaths(ways, m, n);
}
private int uniquePaths(int[][] ways, int m, int n) {
if (ways[m][n] > 0) {
return ways[m][n];
}
if (m * n == 0) {
ways[m][n] = 0;
return 0;
}
if (m == 1 || n == 1) {
ways[m][n] = 1;
return 1;
}
ways[m][n] = uniquePaths(ways, m, n - 1) + uniquePaths(ways, m - 1, n);
return ways[m][n];
}
}