动态规划。巧妙的是通过在最上和最左边加一条0的线,大大简化了编程实现。
public class Solution { public int uniquePaths(int m, int n) { // Start typing your Java solution below // DO NOT write main() function int[][] mx = new int[m+1][n+1]; for (int i = 0; i < m+1; i++) { mx[i][0] = 0; } for (int i = 0; i < n+1; i++) { mx[0][i] = 0; } for (int i = 1; i < m+1; i++) { for (int j = 1; j < n+1; j++) { if (i == 1 && j == 1) mx[i][j] = 1; else mx[i][j] = mx[i-1][j] + mx[i][j-1]; } } return mx[m][n]; } }