题目

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.

Note: m and n will be at most 100.

代码:oj测试通过 Runtime: 48 ms

 class Solution:
# @param obstacleGrid, a list of lists of integers
# @return an integer
def uniquePathsWithObstacles(self, obstacleGrid):
# ROW & COL
ROW = len(obstacleGrid)
COL = len(obstacleGrid[0])
# one row case
if ROW==1:
for i in range(COL):
if obstacleGrid[0][i]==1:
return 0
return 1
# one column case
if COL==1:
for i in range(ROW):
if obstacleGrid[i][0]==1:
return 0
return 1
# visit normal case
dp = [[0 for i in range(COL)] for i in range(ROW)]
for i in range(COL):
if obstacleGrid[0][i]!=1:
dp[0][i]=1
else:
break
for i in range(ROW):
if obstacleGrid[i][0]!=1:
dp[i][0]=1
else:
break
# iterator the other nodes
for row in range(1,ROW):
for col in range(1,COL):
if obstacleGrid[row][col]==1:
dp[row][col]=0
else:
dp[row][col]=dp[row-1][col]+dp[row][col-1] return dp[ROW-1][COL-1]

思路

思路模仿Unique Path这道题:

http://www.cnblogs.com/xbf9xbf/p/4250359.html

只不过多了某些障碍点;针对障碍点,加一个判断条件即可:如果遇上障碍点,那么到途径这个障碍点到达终点的可能路径数为0。然后继续迭代到尾部即可。

个人感觉40行的python脚本不够简洁,总是把special case等单独拎出来。后面再练习代码的时候,考虑如何让代码更简洁。

leetcode 【 Unique Paths II 】 python 实现的更多相关文章

  1. [leetcode]Unique Paths II @ Python

    原题地址:https://oj.leetcode.com/problems/unique-paths-ii/ 题意: Follow up for "Unique Paths": N ...

  2. LeetCode: Unique Paths II 解题报告

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

  3. LEETCODE —— Unique Paths II [动态规划 Dynamic Programming]

    唯一路径问题II Unique Paths II Follow up for "Unique Paths": Now consider if some obstacles are ...

  4. LEETCODE —— Unique Paths II [Dynamic Programming]

    唯一路径问题II Unique Paths II Follow up for "Unique Paths": Now consider if some obstacles are ...

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

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

  6. Leetcode Unique Paths II

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

  7. [Leetcode] unique paths ii 独特路径

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

  8. [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 ...

  9. 动态规划小结 - 二维动态规划 - 时间复杂度 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) 有关 这种情况下,时间 ...

  10. [Leetcode Week12]Unique Paths II

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

随机推荐

  1. iOS 解决tableView中headerView头部视图不跟随tableView滑动的方法

    解决方法如下: if (scrollView.contentOffset.y >= 0 && scrollView.contentOffset.y <= pushNewsT ...

  2. 腾讯云服务器CVM购买详细过程 选择我们需要的腾讯云服务器

    腾讯云服务商有云服务器.云数据库.CDN.云存储等产品,其中较多的用户会选择腾讯云服务器,因为用途比较广泛,比如用来软件的运行以及网站建设,如今一般都是用云服务器,而不是用虚拟主机,毕竟虚拟主机的性价 ...

  3. pat甲级1020中序后序求层序

    1020 Tree Traversals (25)(25 分) Suppose that all the keys in a binary tree are distinct positive int ...

  4. java Vamei快速教程20 GUI

    作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! GUI(Graphical User Interface)提供了图形化的界面,允许 ...

  5. Hbase各种查询总结

    运用hbase好长时间了,今天利用闲暇时间把Hbase的各种查询总结下,以后有时间把协处理器和自定义File总结下. 查询条件分为: 1.统计表数据 2,hbase 简单分页 3,like 查询 4  ...

  6. 在RichTextBox控件中添加超链接文本

    实现效果: 知识运用: RichTextBox控件的AppendText方法 public void AppendText{string textData} //向控件中添加文本内容 和Process ...

  7. kubernetes-控制器Deployment和DaemonSet(八)

    Pod与controllers的关系 •controllers:在集群上管理和运行容器的对象•通过label-selector相关联•Pod通过控制器实现应用的运维,如伸缩,升级等 控制器又称工作负载 ...

  8. cuda流测试=basic_single_stream

    cuda流测试 /* * Copyright 1993-2010 NVIDIA Corporation. All rights reserved. * * NVIDIA Corporation and ...

  9. UNC路径格式

    \\192.168.3.66\c$  访问本地网内计算机

  10. 关于SQL数据库 msdb.dbo.sp_send_dbmail 函数发送邮件的场景分析

    关于SQL数据库 msdb.dbo.sp_send_dbmail 函数发送邮件的场景分析 在推行系统中,时不时会有用户提出希望系统能自动推送邮件,由于手头的工具和能力有限,不少需求都借助于sql se ...