【leetcode】Unique Paths II
来源:互联网 发布:域名怎么解析两个IP 编辑:程序博客网 时间:2024/04/29 09:01
Question
Tips
I think still use dynamic programming.
class Solution {public: int uniquePathsWithObstacles(vector<vector<int> >& obstacleGrid) { int m = obstacleGrid.size(); int n = obstacleGrid[0].size(); if(obstacleGrid[m-1][n-1]!=0) return 0; if(n==1 && m==1) if(obstacleGrid[0][0]==0) return 1; else return 0; int **res; res = new int*[m]; for(int i = 0; i < m; ++i) res[i] = new int[n]; for(int i=0;i<m;++i) for(int j=0;j<n;++j) res[i][j]=0; //Initialize for(int i = n-2; i >= 0; --i) if(obstacleGrid[m-1][i] != 0) break; else res[m-1][i]=1; for(int i = m-2; i >= 0; --i) if(obstacleGrid[i][n-1] != 0) break; else res[i][n-1] = 1; //Bottom to top int row = m-2, col = n-2; while(row>=0 && col>=0){ for(int i = row; i>=0;--i) if(obstacleGrid[i][col] != 0) continue; else{ if(obstacleGrid[i+1][col]==0) res[i][col]+=res[i+1][col]; if(obstacleGrid[i][col+1]==0) res[i][col]+=res[i][col+1]; } for(int i=col-1;i>=0;--i) if(obstacleGrid[row][i]!=0) continue; else{ if(obstacleGrid[row][i+1]==0) res[row][i]+=res[row][i+1]; if(obstacleGrid[row+1][i]==0) res[row][i]+=res[row+1][i]; } --col; --row; } return res[0][0]; }};
0 0
- LeetCode: Unique Paths II
- [Leetcode] Unique Paths II
- LeetCode : Unique Paths II
- [LeetCode] Unique Paths II
- [LeetCode]Unique Paths II
- [leetcode]Unique Paths II
- LeetCode-Unique Paths II
- [leetcode] Unique Paths II
- LeetCode - Unique Paths II
- LeetCode:Unique Paths II
- 【LeetCode】Unique Paths II
- leetcode Unique Paths II
- LeetCode Unique Paths II
- LeetCode | Unique Paths II
- Leetcode: Unique Paths II
- LeetCode Unique Paths II
- Unique Paths II -- LeetCode
- [LeetCode] Unique Paths II
- 香港验性别费用
- 香港验血测胎儿性别多少钱?
- 为什么怀孕6周不能去香港验血测性别?
- 香港诊所抽血转介化验所进行性别鉴定
- 香港无创DNA胎儿亲子鉴定费用
- 【leetcode】Unique Paths II
- Ljava.lang.Object; cannot be cast to com.bdqn.hibernate.entity.*
- XMPP协议分析-原理篇
- Eclipse快捷键指南
- DNS到底是干什么用的
- 香港性别鉴定准确吗
- 香港血液鉴定性别
- 香港血液性别鉴定
- 香港验血鉴定胎儿性别