不同路径 (Unique Paths)

 

思路:

// @Title: 不同路径 (Unique Paths)
// @Author: qisiii
// @Date: 2024-09-22 23:18:21
// @Runtime: 0 ms
// @Memory: 39.5 MB
// @comment: 
// @flag: 
class Solution {
    public int uniquePaths(int m, int n) {
        int[][] dp = new int[m][n];
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                //但是初始化值没想好,应该第一行和第一列都是1条路径
                if (i == 0) {
                    dp[i][j] = 1;
                } else if (j == 0) {
                    dp[i][j] = 1;
                } else {
                    //公式好理解
                    dp[i][j] = dp[i][j - 1] + dp[i - 1][j];
                }
            }
        }

        return dp[m - 1][n - 1];
    }
}
Licensed under CC BY-NC-SA 4.0
最后更新于 2024-10-18