[LeetCode] Unique Paths 2
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) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int m=obstacleGrid.size();
int n=obstacleGrid[0].size();
vector<vector<int>> paths(m,vector<int>(n,0));
if(obstacleGrid.at(0).at(0)==1)
{
return 0;
}
else{
paths.at(0).at(0)=1;
}
for(int i=1;i<m;i++)
{
if(obstacleGrid.at(i).at(0)==0 && paths.at(i-1).at(0)==1)
paths.at(i).at(0)=1;
}
for(int i=1;i<n;i++)
{
if(obstacleGrid.at(0).at(i)==0 && paths.at(0).at(i-1)==1)
paths.at(0).at(i)=1;
}
for(int i=1;i<m;i++)
{
for(int j=1;j<n;j++)
{
if(obstacleGrid.at(i).at(j)==1)
paths.at(i).at(j)=0;
else
paths.at(i).at(j)=paths.at(i).at(j-1)+paths.at(i-1).at(j);
}
}
return paths.at(m-1).at(n-1);
}
};
[LeetCode] Unique Paths 2的更多相关文章
- LeetCode: Unique Paths II 解题报告
Unique Paths II Total Accepted: 31019 Total Submissions: 110866My Submissions Question Solution Fol ...
- [LeetCode] 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 I & II & Minimum Path Sum
Title: https://leetcode.com/problems/unique-paths/ A robot is located at the top-left corner of a m ...
- [leetcode]Unique Paths II @ Python
原题地址:https://oj.leetcode.com/problems/unique-paths-ii/ 题意: Follow up for "Unique Paths": N ...
- [leetcode]Unique Paths @ Python
原题地址:https://oj.leetcode.com/problems/unique-paths/ 题意: A robot is located at the top-left corner of ...
- [LeetCode] Unique Paths && Unique Paths II && Minimum Path Sum (动态规划之 Matrix DP )
Unique Paths https://oj.leetcode.com/problems/unique-paths/ A robot is located at the top-left corne ...
- Leetcode Unique Paths II
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
- LEETCODE —— Unique Paths II [动态规划 Dynamic Programming]
唯一路径问题II Unique Paths II Follow up for "Unique Paths": Now consider if some obstacles are ...
- 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 ...
随机推荐
- 带符号的char类型取值范围为什么是-128——127
以前经常看到带符号的char类型取值范围是-128——127,今天突然想为什么不是-127——127,-128是怎么来的? 127好理解,char类型是8位,最高位是符号位,0正1负,所以011111 ...
- LDAP研究
一.ldap介绍 ldap是轻量级的文件夹訪问协议.重点是文件夹訪问协议.更为重点的是协议.好吧他是一个协议.也就是一个标准. 那么实现这款协议的软件当中有一款就是openldap. 二.安装wind ...
- 网页制作之html基础学习5-background-position用法
我们知道在用图片作为背景的时候,css要这样写,以div容器举例子,也可以是body.td.p等的背景,道理一样. 代码: div{ background:#FFF url(image) no-rep ...
- jfinal集成spring cxf做webservice服务
链接地址:http://zhengshuo.iteye.com/blog/2154047 废话不说,直接上代码 新增cxf的plugin CXFPlugin package com.jfinal.pl ...
- Eclipse使用技巧总结(二)
七.快速切换打开的文件 Ctrl + F6 八.快速大写.小写转换 Ctrl + Shift + Y Ctrl + Shift + X 九.快速删除光标所在行 Ctrl + D 十.快速复制 Ctrl ...
- C中的链接属性及作用域
如果相同的标识符出现在几个不同的源文件中时,它们是表示相同的实体,还是不同的实体.标识符的链接属性决定如何处理在不同文件中出现的标识符.标识符的作用域与它的链接属性有关. 链接属性一般有三种:exte ...
- ant学习记录(复制-移动-删除-依赖综合测试)+fileset
<?xml version="1.0"?> <project name="targetStudy" default="mkdir&q ...
- linux eclipse中运行android AVD 错误
当使用android的AVD时提示以下错误: Starting emulator for AVD 'NexusOne' ERROR: 32-bit Linux Android emulator bin ...
- HDOJ 2680 Dijkstra
题目大意: 给你一个有向图,一个起点集合,一个终点,求最短路.... 解题思路: 1.自己多加一个超级源点,把起点集合连接到超级源点上,然后将起点与超级源点的集合的路径长度设为0,这样就称为一个n+1 ...
- <Win32_12>程序员求爱的创意程序——升级版^_^
前段时间,我编写了一个创意程序,并用于向自己目前的女朋友表白,结果效果还不错,得到了她的芳心. 于是我将自己的创意程序共享到csdn资源上,大多数网友认为创意不错,就是简单了些——呵呵,其实我个人也这 ...