[LeetCode]Unique Paths II
2015-11-26 00:36
537 查看
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
in the grid.
For example,
There is one obstacle in the middle of a 3x3 grid as illustrated below.
The total number of unique paths is
动态规划,注意把受阻塞的置0.
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
1and
0respectively
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 is
2.
动态规划,注意把受阻塞的置0.
class Solution { public: int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) { int rowlen = obstacleGrid.size(); int colen = obstacleGrid[0].size(); vector<vector<int>> Dp(rowlen,vector<int>(colen,0)); for(int i=0; i<rowlen; ++i){ if(obstacleGrid[i][0] == 1) break; Dp[i][0] = 1; } for(int i=0; i<colen; ++i){ if(obstacleGrid[0][i] == 1) break; Dp[0][i] = 1; } for(int i=1; i<rowlen; ++i){ for(int j=1; j<colen; ++j){ if(obstacleGrid[i][j] == 1) Dp[i][j] = 0; else Dp[i][j] = Dp[i-1][j]+Dp[i][j-1]; } } return Dp[rowlen-1][colen-1]; } };
相关文章推荐
- 【Arduino】开发入门教程【五】Hello Arduino
- 2015年11月26日 Java基础系列(一)之String与StringBuffer与StringBuilder的区别
- java 面试专题(二)String StringBuffer StringBuilder的区别
- 可以直接拿来用的常见Query代码片段
- UI基础----CATransition 转场动画 type 私有 API
- 49 You enable block change tracking. You issue the following command: BACKUP INCREMENTAL LEVEL 0 DA
- 47 You want to use RMAN to create compressed backups. Which statement is true about the compression
- miui显示虚拟按键方法
- 45 You want to disable resumable space allocation for all sessions. Which value should be assigned t
- IOS UIToolbar
- iOS UICollectionView实用练习 —— HERO博客
- RequestDispatcher跳转到jsp页面中文乱码
- UIView之基础控件综合事例
- hinernate中用uuid作为自增长类型自动创建表时报错问题记录
- BlockingQueue的使用
- UITextField 限制输入长度 (动态监听属性变化)
- UITableViewCell长按显示UIMenuController
- iOS之UIButton无法显示文字
- UITableViewCell之微博篇
- IOS中UITableViewCell的重用机制原理