美文网首页
63. Unique Paths II

63. Unique Paths II

作者: jecyhw | 来源:发表于2019-05-30 10:31 被阅读0次

    题目链接

    https://leetcode.com/problems/unique-paths-ii/submissions/

    解题思路

    简单dp,直接看代码

    代码

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

    相关文章

      网友评论

          本文标题:63. Unique Paths II

          本文链接:https://www.haomeiwen.com/subject/uhlmtctx.html