Leetcode133: Unique Paths II
2015-11-01 17:26
381 查看
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
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.
class Solution { public: int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) { int m = obstacleGrid.size(); if(m == 0) return 0; int n = obstacleGrid[0].size(); vector<vector<int>> f(m, vector<int>(n)); //初始化行 int i; for(i = 0; i < m; i++) { if(obstacleGrid[i][0] == 1) break; else f[i][0] = 1; } for(; i< m; i++) { f[i][0] == 0; } //初始化列 for(i = 0; i < n; i++) { if(obstacleGrid[0][i] == 1) break; else f[0][i] = 1; } for(; i< n; i++) { f[0][i] == 0; } //递推 for(i = 1; i < m; i++) { for(int j = 1; j < n; j++) { f[i][j] = obstacleGrid[i][j] == 1 ? 0:f[i-1][j] + f[i][j-1]; } } return f[m-1][n-1]; } };
相关文章推荐
- 2015ACM/ICPC亚洲区长春站-重现赛(感谢东北师大)HDU5538 House Building
- iOS--- 如何获得UIView的父控制器
- hdu 5538House Building(长春现场赛)
- 如何清除或初始化 easyui 的 treegrid 控件的数据
- unknwn filesystem entering rescue mode grup rescue解决办法
- setClickable(true)不起作用
- 【iOS】UIAlertView 点击跳转事件
- uIP使用记录
- [sicily]1443. Printer Queue
- UIScrollView 侧滑的手势冲突怎么解决?
- request.getRequestDispatcher()的两个方法forward()/include()!!!
- EasyUI - DataGrid 组建 - [ 搜索功能 ]
- hdu 5538 House Building 基础题
- easyui-datagrid的一些操作记录
- hdu1297 Children’s Queue (动态规划,n个人排成一列的方案数+高精加)
- 消息队列机制messageQuene
- queue的用法
- requirejs项目部署
- required定义
- OC 学习笔记4 NSSet NSMutableSet NSDictionary NSMutableDictionary NSNumber NSValue