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?
思路:第0行和第0列都只有1条路径。对于p(i,j) (1 <= i < m, 1 <= j < m),其路径数为 p(i - 1,j) + p (i, j - 1).
1 public class Solution { 2 /** 3 * @param n, m: positive integer (1 <= n ,m <= 100) 4 * @return an integer 5 */ 6 public int uniquePaths(int m, int n) { 7 if (m == 0 || n == 0) { 8 return 0; 9 } 10 int [][] a = new int[m][n]; 11 for (int i = 0; i < m; i++) { 12 a[i][0] = 1; 13 } 14 for (int i = 1; i < n; i++) { 15 a[0][i] = 1; 16 } 17 for (int i = 1; i < m; i++) { 18 for (int j = 1; j < n; j++) { 19 a[i][j] = a[i - 1][j] + a[i][j - 1]; 20 } 21 } 22 return a[m - 1][n - 1]; 23 } 24 }
原文:http://www.cnblogs.com/FLAGyuri/p/5324913.html