Unique Paths II (dp题)
2024-09-08 07:06:28
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.
代码:
class Solution {
public:
int uniquePathsWithObstacles(vector<vector<int> > &obstacleGrid) {
if(obstacleGrid.size()==) return ;
int row=obstacleGrid.size();
int col=obstacleGrid[].size();
int dp[][];
memset(dp,,sizeof(dp));
bool rblockTag=false;
bool cblockTag=false;
if(obstacleGrid[][]==||obstacleGrid[row-][col-]==)
return ;
for(int i=;i<col;++i){
if(obstacleGrid[][i]==) rblockTag=true;
if(!rblockTag) dp[][i]=;
else dp[][i]=;
}
for(int j=;j<row;++j){
if(obstacleGrid[j][]==) cblockTag=true;
if(!cblockTag) dp[j][]=;
else dp[j][]=;
}
for(int i=;i<row;++i){
for(int j=;j<col;++j){
if(obstacleGrid[i][j]==) dp[i][j]=;
else dp[i][j]=dp[i][j-]+dp[i-][j];//i和j是从1开始
}
}
return dp[row-][col-];
}
};
最新文章
- Tanks坦克大战
- Ubuntu 14.04 安装 JDK 8,ubuntu14.04
- ADF_Data Binding系列2_使用URL Service Data Control
- 免费下载:用于原型设计的 iOS 7 线框图
- IO流的练习2 —— 复制单级文件夹中的文件
- 关于Haproxy安装和配置:负载配置【haproxy.cfg】问题记录
- Storm安装与实验
- C++的双冒号(域解析符)
- 给Jquery添加alert,prompt方法,类似系统的Alert,Prompt,可以响应键盘,支持拖动
- Android 线程Thread的2种实现方法
- Java-Web监听器
- SQL Server自动化运维系列 - 监控磁盘剩余空间及SQL Server错误日志(Power Shell)
- 客户端禁用cookie情况下的URL重写
- javascript中事件对象注册与删除
- 关于jmeter读取CSV文件的详细设置
- collections 模块之Counter
- AngularJs创建一个带参数的自定义方法
- openstack vm ping 114.114.114.114
- Mac上 python 找不到 yaml模块
- linux实现开机自启动脚本