LeetCode63 Unique Paths II
题目:
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. (Medium)
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.
分析:
上一题的follow-up,加上了障碍物,思路一样,只是障碍物处dp[i][j] = 0;
代码:
class Solution {
public:
int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) {
int m = obstacleGrid.size();
int n = obstacleGrid[].size();
int dp[m][n];
memset(dp,,sizeof(dp));
for (int i = ; i < n; ++i) {
if (obstacleGrid[][i] == ) {
break;
}
dp[][i] = ;
}
for (int i = ; i < m; ++i) {
if (obstacleGrid[i][] == ) {
break;
}
dp[i][] = ;
}
for (int i = ; i < m; ++i) {
for (int j = ; j < n; ++j) {
if (obstacleGrid[i][j] == ) {
dp[i][j] = ;
}
else {
dp[i][j] = dp[i - ][j] + dp[i][j - ];
}
}
}
return dp[m - ][n - ];
}
};
注:为什么这道题里dp[m][n] = {0}不能把二维数组初始化为0了......
LeetCode63 Unique Paths II的更多相关文章
- leetcode-63. Unique Paths II · DP + vector
题面 A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). ...
- Leetcode63.Unique Paths II不同路径2
一个机器人位于一个 m x n 网格的左上角 (起始点在下图中标记为"Start" ). 机器人每次只能向下或者向右移动一步.机器人试图达到网格的右下角(在下图中标记为" ...
- LEETCODE —— Unique Paths II [动态规划 Dynamic Programming]
唯一路径问题II Unique Paths II Follow up for "Unique Paths": Now consider if some obstacles are ...
- 62. Unique Paths && 63 Unique Paths II
https://leetcode.com/problems/unique-paths/ 这道题,不利用动态规划基本上规模变大会运行超时,下面自己写得这段代码,直接暴力破解,只能应付小规模的情形,当23 ...
- 【leetcode】Unique Paths II
Unique Paths II Total Accepted: 22828 Total Submissions: 81414My Submissions Follow up for "Uni ...
- 61. Unique Paths && Unique Paths II
Unique Paths A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagra ...
- LeetCode: Unique Paths II 解题报告
Unique Paths II Total Accepted: 31019 Total Submissions: 110866My Submissions Question Solution Fol ...
- 【LeetCode练习题】Unique Paths II
Unique Paths II Follow up for "Unique Paths": Now consider if some obstacles are added to ...
- LeetCode之“动态规划”:Minimum Path Sum && Unique Paths && Unique Paths II
之所以将这三道题放在一起,是因为这三道题非常类似. 1. Minimum Path Sum 题目链接 题目要求: Given a m x n grid filled with non-negative ...
随机推荐
- thinkphp用swiftmailer发邮件demo
QQ邮箱 include_once APPPATH . 'libraries/swiftmailer/swift_required.php'; $transport = Swift_SmtpTrans ...
- 傳説中的 jsonp
jsonp的由來 1 . 網頁上的東西衹要跨域了就不能傳送或者接受數據了.不管是什麽衹要是跨域.Ajax直接请求普通文件存在跨域无权限访问的问题, 2 . 但是src這個東西比較厲害了,請求哪裏都可以 ...
- .NET框架之---MEF托管可扩展框架
MEF简介: 今天学习了下MEF框架,MEF,全称Managed Extensibility Framework(托管可扩展框架).MEF是专门致力于解决扩展性问题的框架,MSDN中对MEF有这样一段 ...
- Cesium 1.50重量级新功能测评
概要 既Cesium 1.49中3dtile加载性能大幅提升以后,Cesium 1.50再次迎来几个重量级新功能: 1 地球裁切,这下相当于可以截取一部分地形影像数据,当作一个平面场景来用了! 2 射 ...
- java窗体swing使用jlabel显示图片
Icon icon = new ImageIcon("src\\resource\\" + jTFimgName.getText()); jLabColor.setIcon(ico ...
- hystrix熔断器
在一个具有多服务的应用中,假如由于其中某一个服务出现问题,导致响应速度变慢,或是根本没有响应返回,会导致它的服务消费者由于长时间的等待,消耗尽线程,进而影响到对其他服务的线程调用,进而会转变为整个应用 ...
- Thread.sleep( ) vs Thread.yield( )
Thread.sleep() The current thread changes state from Running to Waiting/Blocked as shown in the diag ...
- Python 模块chardet安装过程(windows环境)
最近需要一个txt文件的批量转码功能,在网上找到一段批量处理java源文件的py程序如下: #-*- coding: utf-8 -*- import codecs import os import ...
- List分页
listObj.Skip((pagecount-1)*pagesize).Take(pagesize) 假设你每页10条数据当前是第3页 跳到第4页则:listObj.Skip((4-1)*10).T ...
- 【时光回溯】【JZOJ3567】【GDKOI2014】石油储备计划
题目描述 输入 输出 对于每组数据,输出一个整数,表示达到"平衡"状态所需的最小代价. 样例输入 2 3 6 1 5 1 2 1 2 3 2 5 4 5 4 3 2 1 3 1 1 ...