题目来源:

  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. spring+springMVC集成(annotation方式)

    spring+springMVC集成(annotation方式) SpringMVC+Spring4.0+Hibernate 简单的整合 MyBatis3整合Spring3.SpringMVC3

  2. python 入门快速学习整理

    Python 入门学习 1  : 对象类型 1 1.1 列表 1 1.2 字典 2 1.3 元组 2 1.4 元组 2 1.4 文件 3 2  : 条件和循环语句 3 2.1  if else语句 3 ...

  3. j详细说明ava于clone办法

    原文地址:http://leihuang.org/2014/11/14/java-clone/ In java, it essentially means the ability to create ...

  4. cc2540 cc2541 低功耗实測和总结-与注意事项 - 低功耗小于10uA

    CC2541 CC2540 实现超低功耗是很重要的: 我们来总结一下实现方法: 1,有定时器在跑时会一直跑在  PM2  电流在  300uA左右.    没有定时器跑后会到 PM3 , 电流会少于1 ...

  5. android 向serverGet和Post请求的两种方式,android向server发送文件,自己组装协议和借助第三方开源

    一个适用于Android平台的第三方和apache的非常多东西类似,仅仅是用于Android上 我在项目里用的是这个 https://github.com/loopj/android-async-ht ...

  6. linux网络编程之网络函数详解

    1.epoll_create函数 函数声明:int epoll_create(int size) 该 函数生成一个epoll专用的文件描述符.它其实是在内核申请一空间,用来存放你想关注的socket ...

  7. Tomcat启动报Error listenerStart错误

    http://xpenxpen.iteye.com/blog/1545648 今天启动Tomcat启动不了,报以下错: org.apache.catalina.core.StandardContext ...

  8. java动手动脑课后思考题

    public class SquareInt { public static void main(String[] args) { int result; ; x <= ; x++) { res ...

  9. AC自动机妙用

    理解题意之后,很自然的想到了用AC自动机搞,结果网上一搜,全是暴搜,按照自己的思想,AC自动机搞起,果然在提交了数次之后,看到了Accept. AC自动机需要三个步骤: 第一步:建立字典树: 第二步: ...

  10. html 浮动元素

    在CSS布局中分为内联元素(display:inline)和块状元素(display:block),块状元素默认会占据一行,可设置高度宽度以及边距,而内联元素不会也不能设置.常见的内联元素有:a.sp ...