[LeetCode]Unique Paths II
2013-06-03 09:08
387 查看
class Solution { //DP //if a[i][j] is a obstacle, then f[i][j]=0 public: int uniquePathsWithObstacles(vector<vector<int> > &obstacleGrid) { // Start typing your C/C++ solution below // DO NOT write int main() function int m = obstacleGrid.size(); if(0 == m) return 0; int n = obstacleGrid[0].size(); vector<vector<int>> f(m+1, vector<int>(n+1, 0)); if(obstacleGrid[0][0] != 1) f[1][1] = 1; for (int i = 1; i <= m; ++i) { for (int j = 1; j <= n; ++j) { if(i == 1 && j == 1) continue; if(0 == obstacleGrid[i-1][j-1])//empty f[i][j] = f[i-1][j]+f[i][j-1]; } } return f[m] ; } };
second time
class Solution { public: int uniquePathsWithObstacles(vector<vector<int> > &obstacleGrid) { // Start typing your C/C++ solution below // DO NOT write int main() function int m = obstacleGrid.size(); if(m == 0) return 0; int n = obstacleGrid[0].size(); vector<vector<int> > f(m+1, vector<int>(n+1, 0)); for(int i = 1; i <= m; ++i) { for(int j = 1; j <= n; ++j) { if(obstacleGrid[i-1][j-1] == 1) continue; if(i == 1 && j == 1) f[i][j] = 1; else f[i][j] = f[i-1][j]+f[i][j-1]; } } return f[m] ; } };
相关文章推荐
- 【leetcode刷题笔记】Unique Paths II
- [LeetCode] Unique Paths II
- LeetCode_OJ【63】Unique Paths II
- LeetCode | Unique Paths I,II
- LeetCode_63---Unique Paths II
- [leetcode][DP] Unique Paths II
- 【LeetCode】Unique Paths II
- Leetcode Unique Paths II
- LeetCode Unique Paths II(dp)
- 【leetcode】Unique Paths I & II
- [LeetCode] Unique Paths、Unique Paths II、Minimum Path Sum
- LeetCode - Unique Paths II - Frequent
- leetcode -- Unique Paths II
- leetcode63-Unique Paths II
- [LeetCode] Unique Paths && Unique Paths II && Minimum Path Sum (动态规划之 Matrix DP )
- [Leetcode]Unique Paths II
- Leetcode[63]-Unique Paths II
- leetcode_question_63 Unique Paths II
- Leetcode 63:Unique Paths II
- [LeetCode] Unique Paths II