LeetCode题解之Unique Paths II
1、题目描述
2、问题描述
使用动态规划算法,加上条件检测即可
3、代码
1 int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) { 2 int m = obstacleGrid.size() ; 3 int n = obstacleGrid[0].size() ; 4 5 vector<vector<int>> sum(m , vector<int>(n, 0)); 6 bool isOb = false; 7 for( int i = 0; i < m; i++){ 8 if( obstacleGrid[i][0] == 1) isOb = true; 9 if( isOb ){ 10 sum[i][0] = 0; 11 } 12 else{ 13 sum[i][0] = 1; 14 } 15 } 16 17 isOb = false; 18 for(int j = 0; j < n; j++){ 19 if( obstacleGrid[0][j] == 1) isOb = true; 20 if( isOb ){ 21 sum[0][j] = 0; 22 } 23 else{ 24 sum[0][j] = 1; 25 } 26 } 27 28 for(int i = 1; i < m; i++){ 29 for( int j = 1; j < n; j++){ 30 if( obstacleGrid[i][j] == 1) 31 sum[i][j] == 0; 32 else 33 sum[i][j] = sum[i-1][j] + sum[i][j-1]; 34 } 35 } 36 37 return sum[m-1][n-1]; 38 }
pp