跳至主要內容

62. 不同路径

T4mako算法数学动态规划组合数学小于 1 分钟

62. 不同路径

中等

题目描述open in new window

解题思路:
dp,画表,找出递推关系式
if j == 0 && i == 0 -> a[i][j] = 0
else if j == 0 || i == 0 -> a[i][j] = 1
else a[i][j] = a[i-1][j] + a[i][j-1]

class Solution {
    public int uniquePaths(int m, int n) {
        if(m == 1 && n == 1) return 1;
        int[][] a = new int[m][n];
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if(j == 0 && i == 0) a[i][j] = 0;
                else if(j == 0 || i == 0) a[i][j] = 1;
                else  a[i][j] = a[i-1][j] + a[i][j-1];
            }
        }
        return a[m-1][n-1];
    }
}
评论
  • 按正序
  • 按倒序
  • 按热度
Powered by Waline v2.15.5