题目来源:

  https://leetcode.com/problems/unique-paths-ii/


题意分析:

  这题的规则和上一题一样。给一个m×n的矩阵0,1矩阵。0代表可以经过,1代表不可以通过。返回从(0,0)到(m,n)一共有多少种走法。


题目思路:

  这题如果还是用组合的方法做将会非常复杂,所以组合的方法不再考虑。不难发现,从(0,0)到(i,j)的所有可能等于(0,0)到(i - 1,j)和(0,0)到(i,j-1)的和。那么建立一个m×n的表a,a[i][j]代表(0,0)到(i,j)的走法。把表填满,就可以得到结果。时间复杂度是O(m×n)


代码(python):

  

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

转载请注明出处:http://www.cnblogs.com/chruny/p/5008277.html

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

  1. 【LeetCode】063. Unique Paths II

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

  2. Java for LeetCode 063 Unique Paths II

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

  3. LeetCode(63)Unique Paths II

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

  4. 063 Unique Paths II 不同路径 II

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

  5. [Leetcode Week12]Unique Paths II

    Unique Paths II 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/unique-paths-ii/description/ Descrip ...

  6. LeetCode: Unique Paths II 解题报告

    Unique Paths II Total Accepted: 31019 Total Submissions: 110866My Submissions Question Solution  Fol ...

  7. [LeetCode] Unique Paths && Unique Paths II && Minimum Path Sum (动态规划之 Matrix DP )

    Unique Paths https://oj.leetcode.com/problems/unique-paths/ A robot is located at the top-left corne ...

  8. 动态规划小结 - 二维动态规划 - 时间复杂度 O(n*n)的棋盘型,题 [LeetCode] Minimum Path Sum,Unique Paths II,Edit Distance

    引言 二维动态规划中最常见的是棋盘型二维动态规划. 即 func(i, j) 往往只和 func(i-1, j-1), func(i-1, j) 以及 func(i, j-1) 有关 这种情况下,时间 ...

  9. leetcode 62. Unique Paths 、63. Unique Paths II

    62. Unique Paths class Solution { public: int uniquePaths(int m, int n) { || n <= ) ; vector<v ...

  10. Leetcode之动态规划(DP)专题-63. 不同路径 II(Unique Paths II)

    Leetcode之动态规划(DP)专题-63. 不同路径 II(Unique Paths II) 初级题目:Leetcode之动态规划(DP)专题-62. 不同路径(Unique Paths) 一个机 ...

随机推荐

  1. HelloX项目github协同开发指南

    概述 为了提高协同开发效率,HelloX项目已托管到github网站上.根据目前的开发进展,创建了下列几个子项目: HelloX操作系统内核项目:https://github.com/hellox-p ...

  2. openStack windows2008 centos6.* img

    1,下载 windows2008 系统iso介质包 http://download.microsoft.com/download/F/3/8/F384E78B-8F1D-42A6-A308-63E45 ...

  3. Dropping Balls (二叉树+思维)

      Dropping Balls  A number of K balls are dropped one by one from the root of a fully binary tree st ...

  4. Lable 控件 -- 用代码改变要显示字体的颜色

    lable控件怎么改变显示字体的颜色 代码如下: string color = "#B72C34"; this.lbl.ForeColor = System.Drawing.Col ...

  5. hdu acm 2154(多解取一解)

    //题目中结果有一条限制就是最后必须跳回A,如果我们的思想框在这个条件上就很容易卡住,因为这样的条件下的路径很难有规律的罗列,然而我们说这个图形中有三个区域,我们算出每个区域的第n-1次的种类数,然后 ...

  6. window.parent与window.opener的区别与使用

    window.parent 是iframe页面调用父页面对象 举例: a.html 如果我们需要在b.html中要对a.html中的username文本框赋值(就如很多上传功能,上传功能页在ifrma ...

  7. Find the k-th Smallest Element in the Union of Two Sorted Arrays

    (http://leetcode.com/2011/01/find-k-th-smallest-element-in-union-of.html) Given two sorted arrays A, ...

  8. parquet code demo

    http://www.programcreek.com/java-api-examples/index.php?source_dir=hiped2-master/src/main/java/hip/c ...

  9. Linux 中 java 访问 windows共享目录

    有两个方案: 1.将windows共享目录,挂载到linux系统下,通过使用本地目录访问windows共享目录 2.通过samba的java实现包,不过需要开个windows共享目录的账户  http ...

  10. uva 11134 - Fabled Rooks(问题转换+优先队列)

    题目链接:uva 11134 - Fabled Rooks 题目大意:给出n,表示要在n*n的矩阵上放置n个车,并且保证第i辆车在第i个区间上,每个区间给出左上角和右小角的坐标.另要求任意两个车之间不 ...