-
Notifications
You must be signed in to change notification settings - Fork 180
Expand file tree
/
Copy pathq062_UniquePaths.java
More file actions
64 lines (53 loc) · 1.67 KB
/
q062_UniquePaths.java
File metadata and controls
64 lines (53 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package leetcode_algorithm;
/**
A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below).
The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked 'Finish' in the diagram below).
How many possible unique paths are there?
*/
public class q062_UniquePaths {
public static void main(String[] args) {
System.out.println(new q062_UniquePaths().uniquePaths(2, 2));
System.out.println(new q062_UniquePaths().uniquePaths(2, 3));
System.out.println(new q062_UniquePaths().uniquePaths(3, 7));
System.out.println(new q062_UniquePaths().uniquePaths(23, 12));
}
/**
* ½â·¨1(¸öÈ˽ⷨ)
* @param m
* @param n
* @return
*/
public int uniquePaths(int m, int n) {
return unique(m , n , 1 , 1 );
}
private int unique(int m, int n, int x, int y) {
if (x == m || y == n) {
return 1;
}
int result = 2 * unique(m, n, x + 1, y + 1);
if (x + 2 <= m) {
result += unique(m, n, x + 2, y);
}
if (y + 2 <= n) {
result += unique(m, n, x, y + 2);
}
return result;
}
/**
*
* @param m
* @param n
* @return
*/
public int uniquePaths2(int m, int n) {
int[][] grid = new int[m][n];
for(int i = 0; i<m;i++) {
for(int j = 0;j < n;j++) {
if(i == 0 || j == 0)
grid[i][j] = 1;
else grid[i][j] = grid[i][j - 1] + grid[i - 1][j];
}
}
return grid[m - 1][n - 1];
}
}