题目

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 };

GitHub测试程序源码

LeetCode(63)Unique Paths II的更多相关文章

  1. 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). ...

  2. 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 ...

  3. LeetCode(63):不同路径 II

    Medium! 题目描述: 一个机器人位于一个 m x n 网格的左上角 (起始点在下图中标记为“Start” ). 机器人每次只能向下或者向右移动一步.机器人试图达到网格的右下角(在下图中标记为“F ...

  4. LeetCode(90):子集 II

    Medium! 题目描述: 给定一个可能包含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集). 说明:解集不能包含重复的子集. 示例: 输入: [1,2,2] 输出: [ [2], [1 ...

  5. LeetCode(219) Contains Duplicate II

    题目 Given an array of integers and an integer k, find out whether there are two distinct indices i an ...

  6. LeetCode(95) Unique Binary Search Trees II

    题目 Given n, generate all structurally unique BST's (binary search trees) that store values 1-n. For ...

  7. LeetCode(137) Single Number II

    题目 Given an array of integers, every element appears three times except for one. Find that single on ...

  8. LeetCode(96) Unique Binary Search Trees

    题目 Given n, how many structurally unique BST's (binary search trees) that store values 1-n? For exam ...

  9. LeetCode(40) Combination Sum II

    题目 Given a collection of candidate numbers (C) and a target number (T), find all unique combinations ...

随机推荐

  1. Redis操作命令大全

    一.key pattern 查询相应的key (1)redis允许模糊查询key 有3个通配符  *.?.[] (2)randomkey:返回随机key (3)type key:返回key存储的类型 ...

  2. Logstash同步mysql数据库信息到ES

    @font-face{ font-family:"Times New Roman"; } @font-face{ font-family:"宋体"; } @fo ...

  3. Java-String 类的常用方法

    Java 中 String 类的常用方法 Ⅰ String 类提供了许多用来处理字符串的方法,例如,获取字符串长度.对字符串进行截取.将字符串转换为大写或小写.字符串分割等,下面我们就来领略它的强大之 ...

  4. android:process用法

    1.作用 android:process将组件在新进程中运行. 2.应用范围 可以出现在<application>  <activity>, <service>,  ...

  5. mysql配置mha高可用防火墙未关闭报错

    ########################################################################## l  问题背景:检查主从复制之后做高可用报错 [r ...

  6. String的用法——判断功能

    package cn.itcast_03; /* String的判断功能: 1.boolean equals(Object obj):字符串的内容是否相同,区分大小写 2.boolean equals ...

  7. AJPFX总结面向对象特征之一的继承知识

    继 承(面向对象特征之一) 好处: 1:提高了代码的复用性. 2:让类与类之间产生了关系,提供了另一个特征多态的前提.   父类的由来:其实是由多个类不断向上抽取共性内容而来的. java中对于继承, ...

  8. Spring框架学习-搭建第一个Spring项目

    步骤一:下载Spring开发包. 官网:https://spring.io/           下载地址:https://repo.spring.io/libs-release-local/org/ ...

  9. javascript实现弹层效果

    首先,需要有一个按钮来模拟登录: <button id="btnLogin"class="login-btn">登录</button> ...

  10. iOS-UI控件之UIButton

    ---恢复内容开始--- UIButton 既可以显示图片,又可以显示文字,还能随时调整内部位置 系统自带尺寸 storyboard内部调整UIButton属性 状态 监听按钮点击事件 凡是继承自UI ...