题目来源


https://leetcode.com/problems/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.


题意分析


Input:a matrix as list[list[int]]

Output:the number of path

Conditions:从左上角走到左下角,注意有障碍不能通过


题目思路


上一题是用数学方法直接解,这一题用动态规划吧,因为每一步都会基于上一格或者是左一格,动态方程为dp[i][j] = dp[i][j-1] + dp[i-1][j]

注意初始化的时候如果遇到障碍,后面的位置不能初始化为0了


AC代码(Python)


 class Solution(object):
def uniquePathsWithObstacles(self, obstacleGrid):
"""
:type obstacleGrid: List[List[int]]
:rtype: int
"""
m = len(obstacleGrid)
n = len(obstacleGrid[0])
dp = [[0 for i in range(n)] for j in range(m)]
for i in range(n):
if obstacleGrid[0][i] == 1:
break
else:
dp[0][i] = 1 for i in range(m):
if obstacleGrid[i][0] == 1:
break
else:
dp[i][0] = 1 for i in range(1, m):
for j in range(1, n):
if obstacleGrid[i][j] == 0:
dp[i][j] = dp[i - 1][j] + dp[i][j - 1] return dp[m - 1][n - 1]

[LeetCode]题解(python):063-Unique path II的更多相关文章

  1. LeetCode 63. Unique Path II(所有不同路径之二)

    Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...

  2. leetcode63—Unique Path II

    A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...

  3. 【LeetCode】063. Unique Paths II

    题目: Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. H ...

  4. Java for LeetCode 063 Unique Paths II

    Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...

  5. Unique path ii

    Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...

  6. [Leetcode 62]机器人走路Unique Path 动态规划

    [题目] A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below) ...

  7. LeetCode 题解 Search a 2D Matrix II。巧妙!

    [ [1, 4, 7, 11, 15], [2, 5, 8, 12, 19], [3, 6, 9, 16, 22], [10, 13, 14, 17, 24], [18, 21, 23, 26, 30 ...

  8. 063 Unique Paths II 不同路径 II

    这是“不同路径” 的进阶问题:现在考虑网格中有障碍物.那样将会有多少条不同的路径从左上角到右下角?网格中的障碍物和空位置分别用 1 和 0 来表示.例如,如下所示在 3x3 的网格中有一个障碍物.[  ...

  9. LeetCode(63)Unique Paths II

    题目 Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. Ho ...

  10. LeetCode题解之Pascal's Triangle II

    1.题目描述 2.题目分析 题目要求返回杨辉三角的某一行,需要将杨辉三角的某行的全部计算出来. 3.代码实现 vector<int> getRow(int rowIndex) { ) ,) ...

随机推荐

  1. 【BZOJ】1270: [BeijingWc2008]雷涛的小猫(DP+水题)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1270 这完全是一眼题啊,但是n^2的时间挺感人.(n^2一下的级别请大神们赐教,我还没学多少dp优化 ...

  2. RSA密钥的生成与配置

    openssl下载地址http://dldx.csdn.net/fd.php?i=20313208579480&s=ac2e809e168f7d5b8bf1515d3d6b1aa4,或者官方下 ...

  3. 【C语言】09-字符串

    一.字符串简介 * 在Java中,一个字符串可以用String类型来存储 String s = "MJ"; C语言中没有String这种类型.其实字符串就是字符序列,由多个字符组成 ...

  4. 通过JDBC连接hive

    hive是大数据技术簇中进行数据仓库应用的基础组件,是其它类似数据仓库应用的对比基准.基础的数据操作我们可以通过脚本方式以hive-client进行处理.若需要开发应用程序,则需要使用hive的jdb ...

  5. ThinkPHP框架的部署

    1.将ThinkPHP框架的框架文件放到想要放置的地方,与创建的应用文件夹同级 2.vhost文件中设置虚拟目录 3.在hosts文件中配置 4.在应用目录中创建入口文件index.php 5.在入口 ...

  6. LINQ Count/Sum/Min/Max/Avg

    参考:http://www.cnblogs.com/peida/archive/2008/08/11/1263384.html Count/Sum/Min/Max/Avg用于统计数据,比如统计一些数据 ...

  7. java 中的instanceof的用法

    instanceof 运算符是Java.php的一个二元操作符(运算符),和==.>.<是同一类东西.由于它是由字母组成的,所以也是Java的保留关键字.它的作用是判断其左边对象是否为其右 ...

  8. 【应用笔记】【AN002】通过iTool2基于MinGW平台读写EEPROM

    为了增加大家 DIY 的乐趣,XiaomaGee今天为大家只做了一篇使用iTool2内置的USB转I2C来读写EEPROM的方法和代码. iTool2简介 iTool2为银杏公司面向电子类研发工程师推 ...

  9. 【新产品发布】EVC8003 磁耦隔离型USB转全功能RS-232

    ============================== 技术论坛:http://www.eeschool.org 博客地址:http://xiaomagee.cnblogs.com 官方网店:h ...

  10. ElasticSearch实战-日志监控平台

    1.概述 在项目业务倍增的情况下,查询效率受到影响,这里我们经过讨论,引进了分布式搜索套件——ElasticSearch,通过分布式搜索来解决当下业务上存在的问题.下面给大家列出今天分析的目录: El ...