Skip to content

Commit ca4a2ba

Browse files
committed
add solution of problem 62: unique path
1 parent c3e5b14 commit ca4a2ba

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

UniquePaths62/README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
A robot is located at the top-left corner of a *m* x *n* grid (marked 'Start' in the diagram below).
2+
3+
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).
4+
5+
How many possible unique paths are there?
6+
7+
![thisImg](http://leetcode.com/wp-content/uploads/2014/12/robot_maze.png)
8+
Above is a 3 x 7 grid. How many possible unique paths are there?
9+
10+
**Note:** *m* and *n* will be at most 100.

UniquePaths62/Solution.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
public class Solution {
2+
public int uniquePaths(int m, int n) {
3+
if (m <= 0 || n <= 0)
4+
return -1;
5+
6+
int[][] result = new int[m][n];
7+
result[0][0] = 1;
8+
9+
for (int i = 1; i < m; i++) {
10+
result[i][0] = 1;
11+
}
12+
13+
for (int i = 1; i < n; i++) {
14+
result[0][i] = 1;
15+
}
16+
17+
for (int i = 1; i < m; i++) {
18+
for (int j = 1; j < n; j++) {
19+
result[i][j] += result[i-1][j] + result[i][j-1];
20+
}
21+
}
22+
23+
return result[m-1][n-1];
24+
}
25+
}

0 commit comments

Comments
 (0)