Skip to content

Commit bc0cd6e

Browse files
author
tanfanhua
committed
dp
1 parent 45e02ee commit bc0cd6e

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.blankj.medium._062;
2+
3+
public class Solution {
4+
5+
public static void main(String[] args) {
6+
Solution s = new Solution();
7+
System.out.println(s.uniquePath(3, 2));
8+
System.out.println(s.uniquePath(7, 3));
9+
}
10+
11+
public int uniquePath(int m, int n) {
12+
if (m <= 0 || n <= 0) {
13+
return -1;
14+
}
15+
16+
int[][] f = new int[m][n];
17+
for (int i = 0; i < m; i++) {
18+
f[i][0] = 1;
19+
}
20+
for (int j = 0; j < n; j++) {
21+
f[0][j] = 1;
22+
}
23+
for (int i = 1; i < m; i++) {
24+
for (int j = 1; j < n; j++) {
25+
f[i][j] = f[i][j - 1] + f[i - 1][j];
26+
}
27+
}
28+
return f[m - 1][n - 1];
29+
}
30+
}

0 commit comments

Comments
 (0)