【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的Bean之Bean的基本概念
从前面我们知道spring其实就是一个大型的工厂,而Spring容器中的Bean就是该工厂的产品.对于Spring容器能够生产那些产品,则取决于配置文件中配置. 对于我们而言,我们使用Spring框架 ...
- GoLang之基础
GoLang之基础 Go是一种并发的.带垃圾回收的.快速编译的语言. 经典的"hello world"入门: package main import "fmt" ...
- 百万级数据查询到datatable中,提示内存溢出
参考资料: http://group.cnblogs.com/topic/32230.html
- oracle 中的round()函数、null值,rownum
round()函数:四舍五入函数 传回一个数值,该数值按照指定精度进行四舍五入运算的结果. 语法:round(number[,decimals]) Number:待处理的函数 Decimals:精度, ...
- sql sever 字符串函数
SQL Server之字符串函数 以下所有例子均Studnet表为例: 计算字符串长度len()用来计算字符串的长度 select sname ,len(sname) from student ...
- Hadoop第3周练习--Hadoop2.X编译安装和实验
作业题目 位系统下进行本地编译的安装方式 选2 (1) 能否给web监控界面加上安全机制,怎样实现?抓图过程 (2)模拟namenode崩溃,例如将name目录的内容全部删除,然后通过secondar ...
- AlwaysOn可用性组测试环境安装与配置(一)--SQL群集环境搭建
一.测试环境介绍 1. 宿主使用工作站(HYPR-V)基本配置如下: 处理器:Intel(R) Core(TM) i5-4470 CPU @ 3.20GHz 3.20GHz 内存(RAM):8.00G ...
- 【PHP面向对象(OOP)编程入门教程】7.特殊的引用”$this“的使用
现在我们知道了如何访问对象中的成员,是通过”对象->成员”的方式访问的,这是在对象的外部去访问对象中成员的形式, 那么如果我想在对象的内部,让对象里的方法访问本对象的属性, 或是对象中的方法去调 ...
- java 1.7
http://superuser.com/questions/740064/how-to-install-java-1-7-runtime-on-macos-10-9-mavericks sudo r ...
- 用CSS画个三角形
<!DOCTYPE html> <html> <head> <style type="text/css"> #trangle { d ...