欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

【动态规划】LeetCode 63. Unique Paths II

程序员文章站 2022-07-12 12:39:35
...

LeetCode 63. Unique Paths II

Solution1:我的答案
在哪里做过这题?

class Solution {
public:
    int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) {
        int m = obstacleGrid.size();
        if (!m) return 0;
        int n = obstacleGrid[0].size();
        vector<vector<int> > dp(m, vector<int> (n, 1));
        for (int i = 0; i < m;) {
            if (obstacleGrid[i][0]) {
                for (; i < m; i++)
                    dp[i][0] = 0;
            }
            i++;
        }
        for (int i = 0; i < n;) {
            if (obstacleGrid[0][i]) {
                for (; i < n; i++)
                    dp[0][i] = 0;
            }
            i++;
        }
        for (int i = 1; i < m; ++i) 
            for (int j = 1; j < n; ++j) { 
                if (obstacleGrid[i][j])
                    dp[i][j] = 0;
                else
                    dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
            }
        return dp[m - 1][n - 1];
    }
};