其他分享
首页 > 其他分享> > leetcode 不同路径 中等

leetcode 不同路径 中等

作者:互联网

 

 

很简单,dp[i][j] = dp[i - 1][j] + dp[i][j - 1];

class Solution {
public:
    int uniquePaths(int m, int n) {
        vector<vector<int>> dp(m);
        for(auto &item : dp) item.resize(n, 0);
        for(int i = 0; i < m; ++ i) {
            for(int j = 0; j < n; ++ j) {
                if(i == 0 && j == 0) {
                    dp[i][j] = 1;
                    continue;
                }
                if(i == 0) dp[i][j] += dp[i][j - 1];
                else if(j == 0) dp[i][j] += dp[i - 1][j];
                else dp[i][j] += dp[i][j - 1] + dp[i - 1][j];
            }
        }
        return dp[m - 1][n - 1];
    }
};

 

标签:uniquePaths,int,路径,中等,++,else,item,leetcode,dp
来源: https://www.cnblogs.com/rookie-acmer/p/15077055.html