题目来源


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. BZOJ1770 : [Usaco2009 Nov]lights 燈

    设$f[i]$表示$i$点按下开关后会影响到的点的集合,用二进制表示. 不妨设$n$为偶数,令$m=\frac{n}{2}$,对于前一半暴力$2^m$搜索所有方案,用map维护每种集合的最小代价. 对 ...

  2. [NOI 2014]做题记录

    [NOI2014]起床困难综合症 按位贪心 #include <algorithm> #include <iostream> #include <cstring> ...

  3. MyEclipse设置注释格式(转载)

    Window --> Java --> Code Style --> Code Templates --> Comments --> types --> Edit ...

  4. OpenCV 2.4.11 VS2012 Configuration

    Add in the system Path: C:\opencv\build\x86\vc11\bin; Project->Project Property->Configuration ...

  5. Odoo Entypo Regular Icon List

    参考地址: http://www.fontslog.com/entypo-regular-otf-33800.htm#custompreview 或 http://www.w3cplus.com/w3 ...

  6. thinkphp框架中session常识

    在看别人代码时候,发现他,在tp框架中使用session没有些session_start();然后我去查看了手册初始化设置方法 无需手动调用,在App类的初始化工作结束后会自动调用,通常项目只需要配置 ...

  7. C#生成日期流水账号

    生成类似 LS14120002 private String GenNewOrder() { String newOrder; var orders = LYOrder.Items; String l ...

  8. 《GK101任意波发生器》升级固件发布(版本:1.0.2build539)

    一.固件说明: 硬件版本:0,logic.3 固件版本:1.0.2.build539 编译日期:2014-10-08 ====================================== 二. ...

  9. Yii2 Format 如何使用

    $formatter = \Yii::$app->formatter; // output: January 1, 2014 echo $formatter->asDate('2014-0 ...

  10. Mybatis分页和Spring的集成

    写了一个Mybatis分页控件,在这记录一下使用方式. 在Maven中加入依赖: ? 1 2 3 4 5 6 7 8 9 <dependencies>   ...     <depe ...