【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). ...
随机推荐
- Java读取txt文件,计算2011年9月份的通话时间
public class test2 { /** * @param args * @throws IOException */ public static void main(String[] arg ...
- 页面多次调用查询文章(have_posts())
通常来说一个页面只调用查询一次文章.have_posts() 如果页面,比如首页需要按照不同的查询参数调用多次文章 需要做如下处理: //loop前 $temp_query = $wp_que ...
- Java7的异常处理新特性-addSuppressed()方法等
开发人员对异常处理的try-catch-finally语句块都比较熟悉.如果在try语句块中抛出了异常,在控制权转移到调用栈上一层代码之前,finally语句块中的语句也会执行.但是finally语句 ...
- JAVA日期加减运算
1.用java.util.Calender来实现 Calendar calendar=Calendar.getInstance(); calendar.setTime(new Date()) ...
- oracle vm virtualbox右ctrl切换显示模式
转自: http://blog.csdn.net/lyc_daniel/article/details/44195515 virtualbox里面有个HOME键,注意这个HOME键不一定是键盘上的HO ...
- django 文件上传
模板文件: <form method='post' action='/script/upload/' enctype="multipart/form-data" accept ...
- pygal and matplotlib(again)
之前项目有用过pygal做chart图, 写代码很容易,几行代码就很做出一个看上去还不错的chart, 缺点是: 要调的再美观很难, Web上的交互效果较差. 在web上做可视化还是推荐采用Echar ...
- Sphinx扩展安装安装
Coreseek官方教程中建议php使用直接include一个php文件进行操作,事实上php有独立的sphinx模块可以直接操作coreseek(coreseek就是sphinx!)已经进入了php ...
- 【CISP笔记】安全漏洞与恶意代码(2)
恶意代码自我保护 进程保护 进程守护 超级权限 检测对抗 反动态调试 反静态调试 恶意代码检测技术 特征码扫描 沙箱技术 行为检测 恶意代码分析技术 静态分析 需要实际执行恶意代码,它通过对其二进制文 ...
- 使用原生JS封装Ajax
使用原生 的JS封装 Ajax,实现 仿JQuery的Ajax,post,get三种异步请求方式: var MAjax = { //根据浏览器创建异步对象 createXhr: function () ...