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.

障碍物对应的dp表中设为0即可,为了使代码简洁,多加了两行dp[i][0]和dp[0][j]

 class Solution(object):
def uniquePathsWithObstacles(self, obstacleGrid):
m,n = len(obstacleGrid),len(obstacleGrid[0])
dp = [[0 for j in range(n+1)] for i in range(m+1)]
dp[0][1] = 1
for i in range(1,m+1):
for j in range(1,n+1):
if not obstacleGrid[i-1][j-1]:
dp[i][j] = dp[i][j-1] + dp[i-1][j]
return dp[m][n]

还有一种占用O(N)空间的方法,感觉挺不错的

思路:用一个数组tmp记录每一行中每一个位置拥有的次数,每到一个没有障碍物的位置,那么这个位置自动继承上一个位置上所拥有的次数

 class Solution(object):
def uniquePathsWithObstacles(self, obstacleGrid):
m,n=len(obstacleGrid),len(obstacleGrid[0])
tmp = [0]*(n+1)
tmp [n-1] = 1
for i in range(m-1,-1,-1):
for j in range(n-1,-1,-1):
if obstacleGrid[i][j]:
tmp[j] = 0
else:
tmp[j] += tmp [j+1]
return tmp[0]

[leetcode DP]63. Unique Paths II的更多相关文章

  1. 【一天一道LeetCode】#63. Unique Paths II

    一天一道LeetCode (一)题目 Follow up for "Unique Paths": Now consider if some obstacles are added ...

  2. 【LeetCode】63. Unique Paths II

    Unique Paths II Follow up for "Unique Paths": Now consider if some obstacles are added to ...

  3. 【LeetCode】63. Unique Paths II 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/unique-pa ...

  4. LeetCode OJ 63. Unique Paths II

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

  5. leetcode@ [62/63] Unique Paths II

    class Solution { public: int uniquePathsWithObstacles(vector<vector<int>>& obstacleG ...

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

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

  7. 62. Unique Paths && 63 Unique Paths II

    https://leetcode.com/problems/unique-paths/ 这道题,不利用动态规划基本上规模变大会运行超时,下面自己写得这段代码,直接暴力破解,只能应付小规模的情形,当23 ...

  8. 【LeetCode练习题】Unique Paths II

    Unique Paths II Follow up for "Unique Paths": Now consider if some obstacles are added to ...

  9. [LeetCode] 63. Unique Paths II 不同的路径之二

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

随机推荐

  1. 20155227 2016-2017-2 《Java程序设计》第七周学习总结

    20155227 2016-2017-2 <Java程序设计>第七周学习总结 教材学习内容总结 认识时间与日期 时间的度量 世界时:在1972年引入UTC之前,GMT与UT是相同的. 国际 ...

  2. [洛谷P1228]地毯填补问题 题解(分治)

    Description 相传在一个古老的阿拉伯国家里,有一座宫殿.宫殿里有个四四方方的格子迷宫,国王选择驸马的方法非常特殊,也非常简单:公主就站在其中一个方格子上,只要谁能用地毯将除公主站立的地方外的 ...

  3. php中路径斜杠的应用,兼容win与linux

    更多内容推荐微信公众号,欢迎关注: PHP中斜杠的运用 兼容win和linux 使用常量:DIRECTORY_SEPARATOR如:"www".DIRECTORY_SEPARATO ...

  4. VUE常用指令总结!

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  5. dropload的使用记录

    这次做一个H5的页面,需要用到上拉加载,下拉刷新的功能,在网上看到ximen写的dropload.js可以满足需求(此处致谢作者),但是用的时候还是踩了一些坑,这里记录下来备忘. 一些小问题:1. m ...

  6. TCP确认延时和Nagle算法

    TCP确认延时和Nagle算法 nagle 算法是   发送端 收到前一个报文的确认然后再发送下一个tcp数据.这样可以避免大量的小数据. TCP_NODELAY选项控制. Delay ACK是   ...

  7. 深入理解KS

    一.概述 KS(Kolmogorov-Smirnov)评价指标,通过衡量好坏样本累计分布之间的差值,来评估模型的风险区分能力. KS.AUC.PR曲线对比: 1)ks和AUC一样,都是利用TPR.FP ...

  8. WIN下的CMD下载命令

    certutil -urlcache -split -f 远程地址 本地保存的文件跑径与文 件名 # 如里不写本地文 件名与路径名, 会自动跟远程文 件名相同, 并保存到当前目 录下 另一个是: bi ...

  9. jq 监听input值的变化

    $(".popWeiXing .name").bind("input propertychange", function() { modValue.diyDat ...

  10. Floyd_Warshall算法

    Floyd_Warshall算法主要用于求解所有节点对的最短路径,代码如下: #include<iostream> using namespace std; #define Inf 655 ...