[email protected] [62/63] Unique Paths II

 1 class Solution {
 2 public:
 3     int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) {
 4         if(obstacleGrid.size()==0 || obstacleGrid[0].size()==0) return 0;
 5         int m = obstacleGrid.size(), n = obstacleGrid[0].size();
 6         if(obstacleGrid[m-1][n-1]==1) return 0;
 7
 8         vector<vector<int> > dp(m);
 9         for(int i=0;i<dp.size();++i) dp[i].resize(n);
10         for(int i=0;i<dp.size();++i){
11             for(int j=0;j<dp[i].size();++j) dp[i][j]=0;
12         }
13
14         dp[0][0] = (obstacleGrid[0][0]==1) ? 0 : 1;
15         for(int i=1;i<dp.size();++i){
16             if(dp[i-1][0] && !obstacleGrid[i][0]) dp[i][0] = 1;
17         }
18         for(int j=1;j<dp[0].size();++j){
19             if(dp[0][j-1] && !obstacleGrid[0][j]) dp[0][j] = 1;
20         }
21         for(int i=1;i<dp.size();++i){
22             for(int j=1;j<dp[i].size();++j){
23                 if(i>=1 && !obstacleGrid[i-1][j]) dp[i][j] += dp[i-1][j];
24                 if(j>=1 && !obstacleGrid[i][j-1]) dp[i][j] += dp[i][j-1];
25             }
26         }
27
28         return dp[m-1][n-1];
29     }
30 };
时间: 2024-08-27 10:08:57

[email protected] [62/63] Unique Paths II的相关文章

&amp;lt;LeetCode OJ&amp;gt; 62. / 63. Unique Paths(I / II)

62. Unique Paths My Submissions Question Total Accepted: 75227 Total Submissions: 214539 Difficulty: Medium A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The robot can only move either down or right

LeetCode 62, 63 Unique Paths I+II

62. Unique Paths 空间可以按行优化为 O(n),也可以按列优化为O(m). class Solution { public: int uniquePaths(int m, int n) { int dp[m][n]={0}; dp[0][0]=1; for (int i=0;i<m;++i){ for (int j=0;j<n;++j){ if (i==0&&j==0) continue; dp[i][j]=0; if (j-1>=0) dp[i][j]

&lt;LeetCode OJ&gt; 63. Unique Paths II

63. Unique Paths II My Submissions Question Total Accepted: 55136 Total Submissions: 191949 Difficulty: Medium Follow up for "Unique Paths":紧接着上一题"唯一路劲",现在考虑有一些障碍在网格中,无法到达,请重新计算到达目的地的路线数目 Now consider if some obstacles are added to the

LeetCode --- 63. Unique Paths II

题目链接:Unique Paths II Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How many unique paths would there be? An obstacle and empty space is marked as 1 and 0 respectively in the grid. For example, There is one

63. Unique Paths II(js)

63. Unique Paths II A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (

LeetCode开心刷题二十九天——63. Unique Paths II**

63. Unique Paths II Medium 938145FavoriteShare A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The robot can only move either down or right at any point in time. The robot is trying to reach the bottom

leetCode 63.Unique Paths II (唯一路径II) 解题思路和方法

Unique Paths II Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How many unique paths would there be? An obstacle and empty space is marked as 1 and 0 respectively in the grid. For example, There is one obsta

【一天一道LeetCode】#63. Unique Paths II

一天一道LeetCode (一)题目 Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How many unique paths would there be? An obstacle and empty space is marked as 1 and 0 respectively in the grid. For example, There is one ob

62. 63. Unique Paths 64. Minimum Path Sum

1. A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked 'Finish' i