LeetCode(63)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.
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.
分析
带障碍的路径数计算问题,这个题目与上一题的区别之处在于,m*n的矩阵中有部分设置了障碍,当然有障碍的地方不能通过;
虽然设立了障碍,该题目的本质仍然是一个动态规划问题,我们只需要增加判断当前点是否有障碍的代码即可,若有障碍那么此处不能通行,自然f(i,j)=0,对于其他点,依然用上一题目的推导公式即可!
AC代码
//直接用非递归算法求解
class Solution {
public:
int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) {
if (obstacleGrid.empty())
return 0;
int m = obstacleGrid.size();
int n = obstacleGrid[0].size();
vector<vector<int> > ret(m, vector<int>(n, 0));
//矩阵首列
for (int i = 0; i < m; i++)
{
//无障碍,则有一条路径,否则不通
if (obstacleGrid[i][0] != 1)
ret[i][0] = 1;
else
break;
}//for
//矩阵首行
for (int j = 0; j < n; j++)
{
//无障碍,则有一条路径,否则不通
if (obstacleGrid[0][j] != 1)
ret[0][j] = 1;
else
break;
}//for
//其余位置
for (int i = 1; i < m; i++)
{
for (int j = 1; j < n; j++)
{
//当前位置为障碍,则到此处路径数为0
if (obstacleGrid[i][j] == 1)
ret[i][j] = 0;
else{
ret[i][j] = ret[i][j - 1] + ret[i - 1][j];
}//else
}//for
}//for
return ret[m - 1][n - 1];
}//uniques
};
LeetCode(63)Unique Paths II的更多相关文章
- LeetCode(62)Unique Paths
题目 A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). ...
- LeetCode(113) Path Sum II
题目 Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given ...
- LeetCode(63):不同路径 II
Medium! 题目描述: 一个机器人位于一个 m x n 网格的左上角 (起始点在下图中标记为“Start” ). 机器人每次只能向下或者向右移动一步.机器人试图达到网格的右下角(在下图中标记为“F ...
- LeetCode(90):子集 II
Medium! 题目描述: 给定一个可能包含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集). 说明:解集不能包含重复的子集. 示例: 输入: [1,2,2] 输出: [ [2], [1 ...
- LeetCode(219) Contains Duplicate II
题目 Given an array of integers and an integer k, find out whether there are two distinct indices i an ...
- LeetCode(95) Unique Binary Search Trees II
题目 Given n, generate all structurally unique BST's (binary search trees) that store values 1-n. For ...
- LeetCode(137) Single Number II
题目 Given an array of integers, every element appears three times except for one. Find that single on ...
- LeetCode(96) Unique Binary Search Trees
题目 Given n, how many structurally unique BST's (binary search trees) that store values 1-n? For exam ...
- LeetCode(40) Combination Sum II
题目 Given a collection of candidate numbers (C) and a target number (T), find all unique combinations ...
随机推荐
- 支持宕机自动恢复触发一次性或周期性任务执行的组件包首次介绍-easyTask
easyTask介绍 一个方便触发一次性或周期性任务执行的工具包,支持海量,高并发,高可用,宕机自动恢复任务 使用场景 需要精确到秒的某一时刻触发任务执行.比如订单交易完成24小时后如果客户未评价,则 ...
- 2018SCin tsyzDay1 模拟赛-模拟
预计得分:70+0+0+100+100+100+100=470 实际得分:70+0+0+30+100+0+40=240 第一天就被模拟虐爆qwq T1 https://www.luogu.org/pr ...
- 关于浮动与清浮动 float
浮动常见的几种属性值 float {left; right; none; } 主要是定义元素朝哪个方向浮动: 元素浮动后的特性 在一行显示,父级的宽度放不下,自己折行: 支持宽高等样式: 不设置 ...
- POJ 2833 The Average(优先队列)
原题目网址:http://poj.org/problem?id=2833 本题中文翻译: 描述 在演讲比赛中,当选手完成演讲时,评委将对他的演出进行评分. 工作人员删除最高成绩和最低成绩,并计算其余成 ...
- Coins HDU - 2844 POJ - 1742
Coins HDU - 2844 POJ - 1742 多重背包可行性 当做一般多重背包,二进制优化 #include<cstdio> #include<cstring> in ...
- jquery readio checked
今天太鬼火了为这个难问题搜了一下午了到最后还是csdn的朋友给了我正确的答案http://bbs.csdn.net/topics/300162450谢谢这位朋友 // $("#ISOK1&q ...
- react学习文档
转自http://www.ruanyifeng.com/blog/2015/03/react.html,阮一峰老师的博客. 最近想学习react,官方文档的例子不是那么浅显易懂,看了相关博客,觉得阮一 ...
- Android开发中查看未root真机的app数据库
在Android开发中,如果用到数据库来储存数据,那么难免就要查看数据库中的内容,可是对于未root的真机来说,查看数据库就不是那么容易了,如果仅仅为了查看数据库再把手机root了,有点得不偿失,所以 ...
- 修改xampp的mysql默认密码和端口
修改MySQL默认密码 MySQL 的“root”用户默认状态是没有密码的,所以在 PHP 中您可以使用 mysql_connect("localhost","root& ...
- Node.js——获取文件上传进度
https://juejin.im/post/5a77a46cf265da4e78327552?utm_medium=fe&utm_source=weixinqun