leetcode-62. Unique Paths
题目:
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 Solution {
public int uniquePaths(int m, int n) {
int[][] step = new int[m][n];
for(int j=0;j<n;j++){
step[0][j] = 1;
}
for(int j=0;j<m;j++){
step[j][0] = 1;
}
for(int i = 1; i < m; i++)
for(int j = 1; j < n; j++){
step[i][j] =step[i-1][j]+step[i][j-1];
}
return step[m-1][n-1];
}
}