63. Unique Paths II
来源:互联网 发布:淘宝多少好评一个钻 编辑:程序博客网 时间:2024/05/16 23:01
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 1 and 0 respectively 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.
这道题和62题没啥区别,只要将路障处设为0就可以了
class Solution {public: int uniquePathsWithObstacles(vector<vector<int> >& obstacleGrid) { int m=obstacleGrid.size(); int n=obstacleGrid[0].size(); vector<vector<int> >sum(m,vector<int>(n,1)); int i,j; for(i=0;i<m;i++) for(j=0;j<n;j++) { if(obstacleGrid[i][j]) sum[i][j]=0; else { if(i==0&&j==0) sum[i][j]=1; else if(i==0) sum[i][j]=sum[i][j-1]; else if(j==0) sum[i][j]=sum[i-1][j]; else sum[i][j]=sum[i-1][j]+sum[i][j-1]; } } return sum[m-1][n-1]; }};
0 0
- 63. Unique Paths II
- 63. Unique Paths II
- 63. Unique Paths II
- 63. Unique Paths II
- 63. Unique Paths II
- 63. Unique Paths II
- 63. Unique Paths II
- 63. Unique Paths II
- 63. Unique Paths II
- 63. Unique Paths II
- 63. Unique Paths II
- 63. Unique Paths II
- 63. Unique Paths II
- 63. Unique Paths II
- 63. Unique Paths II
- 63. Unique Paths II
- 63. Unique Paths II
- 63. Unique Paths II
- socket
- visual studio 2015key 密匙
- C/C++的内存机制
- 深入理解自定义Annotation,实现ButterKnif小原理
- 蓝牙4.0后台运行
- 63. Unique Paths II
- Eclipse 闪退 有效的方法,
- 玩转nodeJS系列:使用原生API实现简单灵活高效的路由功能(支持nodeJs单机集群),nodeJS本就应该这样轻快
- Android:Layout_weight的深刻理解
- ibatis dtd文件及问题案例
- 如何用几何画板证明椭圆第二定义
- does not implement methodSignatureForSelector: -- trouble ahead
- Bitmap常见优化
- 深入理解Java:SimpleDateFormat安全的时间格式化