【leetcode】Unique Paths II
Unique Paths II
Total Accepted: 22828 Total Submissions: 81414My Submissions
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) {
int m=obstacleGrid.size();
int n=obstacleGrid[].size();
int dp[][];
dp[][]=obstacleGrid[][]==?-:;
for(int i=;i<m;i++)
{
if(obstacleGrid[i][]==)
{
dp[i][]=-;
continue;
}
if(dp[i-][]==-) dp[i][]=-;
else dp[i][]=;
}
for(int j=;j<n;j++)
{
if(obstacleGrid[][j]==)
{
dp[][j]=-;
continue;
}
if(dp[][j-]==-) dp[][j]=-;
else dp[][j]=;
}
for(int i=;i<m;i++)
{
for(int j=;j<n;j++)
{
if(obstacleGrid[i][j]==)
{
dp[i][j]=-;
continue;
}
if(dp[i-][j]==-&&dp[i][j-]==-) dp[i][j]=-;
else if(dp[i-][j]==-&&dp[i][j-]!=-) dp[i][j]=dp[i][j-];
else if(dp[i-][j]!=-&&dp[i][j-]==-) dp[i][j]=dp[i-][j];
else if(dp[i-][j]!=-&&dp[i][j-]!=-) dp[i][j]=dp[i-][j]+dp[i][j-];
}
}
return dp[m-][n-]==-?:dp[m-][n-];
}
};
class Solution {
public:
int uniquePathsWithObstacles(vector<vector<int> > &obstacleGrid) {
int m=obstacleGrid.size();
int n=obstacleGrid[].size();
int dp[][];
//vector<vector<int>> dp(m,vector<int>(n));
dp[][]=obstacleGrid[][]==?:;
for(int i=;i<m;i++)
{
dp[i][]=obstacleGrid[i][]==?:dp[i-][];
}
for(int j=;j<n;j++)
{
dp[][j]=obstacleGrid[][j]==?:dp[][j-];
}
for(int i=;i<m;i++)
{
for(int j=;j<n;j++)
{
dp[i][j]=obstacleGrid[i][j]==?:dp[i-][j]+dp[i][j-];
}
}
return dp[m-][n-];
}
};
【leetcode】Unique Paths II的更多相关文章
- 【题解】【矩阵】【回溯】【Leetcode】Unique Paths II
A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...
- 【Leetcode】【Medium】Unique Paths II
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
- 【题解】【排列组合】【素数】【Leetcode】Unique Paths
A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...
- 【leetcode】Unique Paths
A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...
- 【数组】Unique Paths II
题目: Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. H ...
- 【LeetCode练习题】Unique Paths II
Unique Paths II Follow up for "Unique Paths": Now consider if some obstacles are added to ...
- [Leetcode Week12]Unique Paths II
Unique Paths II 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/unique-paths-ii/description/ Descrip ...
- 【LeetCode】47. Permutations II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:递归 方法二:回溯法 日期 题目地址:htt ...
- LeetCode 63. Unique Paths II不同路径 II (C++/Java)
题目: A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). ...
随机推荐
- spring 容器技术入门
官方文档 翻译 https://waylau.gitbooks.io/spring-framework-4-reference/content/III.%20Core%20Technologies/C ...
- iOS-马上着手开发iOS应用应用程序-第一部分介绍
教程:基础 main 中的 main.m 函数会调用自动释放池 (autorelease pool) 中的 UIApplicationMain 函数. @autoreleasepool { retur ...
- Yii2 rules验证规则
Rules验证规则: required : 必须值验证属性||CRequiredValidator 的别名, 确保了特性不为空. [['字段名1','字段名2'],required] //字段 ...
- JavaScript正则表达式方法总结
str.match(reg) 1.reg没有全局标志g,match将只执行一次匹配.匹配成功返回一个数组,arr = [$0,$1,$2,...,index,str],匹配失败返回null. arr中 ...
- MySQL里的found_row()与row_count()的解释及用法
MySQL中有两个函数来计算上一条语句影响了多少行,不同于SqlServer/Oracle,不要因为此方面的差异而引起功能问题 出处:mysqlpub.com MySQL中有两个函数来计算上一条语 ...
- Linux下中文字符乱码的问题
来源:Linux社区 作者:frankfellow Linux下中文经常会出现乱码,有的是浏览网页出现乱码:有的是文本模式下显示中文出现乱码.下图显示的是我遇到的问题.我安装的是CentOS,x-w ...
- Java字节流:FilterInputStream FilterOutputStream
----------------------------------------------------------------------------------- FilterInputStrea ...
- hdu4950 Monster (水题)
4950 Monster Monster Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others ...
- ThinkPHP3.2.3扩展之生成PDF文件(MPDF)
目前是PHP生成PDF文件最好的插件了,今天介绍下在ThinkPHP3.2.3里如何使用. 先安照路径放好如图. 下面是使用方法 public function pdf(){ //引入类库 Vendo ...
- js 日期处理,json处理
模块化js :requirejshttp://www.requirejs.cn/ 好用的日期控件:http://www.bootcss.com/p/bootstrap-datetimepicker/i ...