leetcode---Unique Paths II---dp
2017-09-15 20:20
429 查看
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 as1and0respectively in the grid.
For example,
There is one obstacle in the middle of a 3x3 grid as illustrated below.
[
[0,0,0],
[0,1,0],
[0,0,0]
]
The total number of unique paths is2.
Note: m and n will be at most 100.
Now consider if some obstacles are added to the grids. How many unique paths would there be?
An obstacle and empty space is marked as1and0respectively in the grid.
For example,
There is one obstacle in the middle of a 3x3 grid as illustrated below.
[
[0,0,0],
[0,1,0],
[0,0,0]
]
The total number of unique paths is2.
Note: m and n will be at most 100.
class Solution { public: int uniquePathsWithObstacles(vector<vector<int> > &obstacleGrid) { int n = obstacleGrid.size(); if(n == 0) return 1; int m = obstacleGrid[0].size(); if(m == 0) return 1; vector<vector<int>> dp(n, vector<int>(m, 0)); for(int i=0; i<n; i++) { if(obstacleGrid[i][0] == 0) dp[i][0] = 1; else break; } for(int j=0; j<m; j++) { if(obstacleGrid[0][j] == 0) dp[0][j] = 1; else break; } for(int i=1; i<n; i++) { for(int j=1; j<m; j++) { if(obstacleGrid[i][j] == 1) { dp[i][j] = 0; continue; } dp[i][j] = dp[i-1][j] + dp[i][j-1]; } } return dp[n-1][m-1]; } };
相关文章推荐
- leetcode -- Unique Paths I &&II-- 典型DP 题目,简单要看
- LeetCode Unique Paths II(dp)
- LeetCode_DP_Unique Paths II
- LeetCode 62/63/120/64 Unique PathsI/II Triangle/Min sum Path/Rectangle Area--DP
- [leetcode][DP] Unique Paths II
- [LeetCode] Unique Paths II(DP)
- Leetcode-Unique Paths II(dp)
- leetcode oj java Unique Paths II
- leetcode_63题——Unique Paths II(动态规划)
- LeetCode Unique Paths(dp)
- LeetCode-63-Unique Paths II(C语言实现)
- LeetCode Unique Paths II
- [leetcode]Unique Paths II
- LeetCode_Unique Paths II
- LeetCode--Unique Paths II
- LeetCode之“动态规划”:Minimum Path Sum && Unique Paths && Unique Paths II
- 开始刷leetcode day60: Unique Paths II
- [LeetCode] Unique Paths II
- [LeetCode]Unique Paths II
- LeetCode刷题笔录Unique Paths II