Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
24 lines (22 loc) · 750 Bytes

File metadata and controls

24 lines (22 loc) · 750 Bytes
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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];
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.