63. Unique Paths II
来源:互联网 发布:天谕凌体型捏脸数据 编辑:程序博客网 时间:2024/06/06 05:09
题目
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.
Note: m and n will be at most 100.
思路
本题和上一篇博客Unique Paths很类似,唯一区别就是,当遇到当前位置为1,需要将res[j]清0.
代码
class Solution {public: int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) { int n = obstacleGrid[0].size(),m = obstacleGrid.size(); if(m==0||m==0) return 0; vector<int> res(n,0); res[0] = 1; for(int i=0;i<m;i++) for(int j=0;j<n;j++) { if(obstacleGrid[i][j]==1) res[j] =0; else { if(j>=1) res[j] += res[j-1]; } } return res[n-1]; }};
阅读全文
1 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
- 拼接sql语句 减少数据库压力
- netty原理详解
- ElasticSearch 5.x & 2.x弹性插件头安装(mac)
- springmvc的数据校验的配置和使用
- Nginx执行php,显示“No input file specified. ”的处理方法
- 63. Unique Paths II
- (3)x210: fix the no output problem at "Starting kernel..."
- vs2015 cuda opengl 混编译 Error"GLboolean" has already been declared in the current scope
- c语言中的基本数据类型
- 遍历QTableWidget中的数据
- 根据两点经纬度计算距离
- 序列化和持久化
- echarts图表大小自适应
- hibernate 关联映射 一对多 和 多对一