Solution Code in c++ Unique Paths II


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

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.