【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). ...
随机推荐
- 大学站防SQL注入代码(ASP版)
方法1: Replace过滤字符 解决方法:查找login.asp下的<from找到下边的类似username=request.Form(”name”) pass=request.Form(”p ...
- F5负载均衡的初识和基本配置
目前全球范围内应用比较广泛的负载均衡设备为美国的F5.F5于2000年底进驻中国,在国内业界,F5负载均衡产品已经成为了主流负载均衡技术的代名词.下面我们对F5负载均衡设备做一个基本介绍,方便大家去认 ...
- linux下的chm阅读器?
pre和code标签是可以同时使用的, 通常pre放在code的前面. 由于 code, pre中不能使用 换行, 段落, 和 尖括号标签, 所以, 对于尖括号, 要换成 html的 实体符号 < ...
- css使 同一行内的 文字和图片 垂直居中对齐?
设置父容器, 使 父容器 div 下的所有元素 都 垂直对齐: father-container *{ vertical-align:middle 找回密码
- 解决前面有一篇文章中'flashplayer.so为什么要设置777权限的'问题 的 思考了
列出某个目录下的所有内容? ls -A, -A等同于-a, 即是-all, 只是-A 不显示.和.. ll ls 某个目录, 如果它下面没有任何东西, 那么 就没有输出! 同时, ll某个目录, 不会 ...
- 导出Excel之Epplus使用教程1(基本介绍)
1.前言 目前Epplus的介绍中文资料很少,我也一直在摸索中使用它,以下是我在使用过程中得到的经验,写出来供大家参考.本系列共4章: 导出Excel之Epplus使用教程1(基本介绍) 导出Exce ...
- 【转】CSS3动画帧数科学计算法
本文来源于:财付通TID 原作者:bboy90 总结都浓缩在这个工具里了,想知道工具的地址或想窥探工具诞生的趣事请往下看 . —————————————————————– 华丽丽的开篇 ...
- VPN添加静态路由表(指定程序或资源走VPN)
在某此情况下,我们希望为VPN客户端指定线路,比如只有公司的资源或网站才使用VPN连接,其它的网络请求依然走他们自己的默认网关. 这种情况下,我们就需要给VPN客户端添加静态路由规则并取消VPN连接的 ...
- Office 2010/2007 简繁体转换按钮不见了?
注:此文章来自微软官方,原文链接:http://support.microsoft.com/kb/2459493/zh-tw 经测试可解决问题.——————————– 通常发生这样的问题,是由于繁简转 ...
- StarWind的安装配置
将安装包拷贝到本地后,执行以下exe文件,默认下一步,直到安装完成. 需汉化版本则将language_chinese.xml文件拷贝到以下路径: C:\Program Files\StarWind S ...