leetcode 【 Unique Paths II 】 python 实现
题目:
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 实现的更多相关文章
- [leetcode]Unique Paths II @ Python
原题地址:https://oj.leetcode.com/problems/unique-paths-ii/ 题意: Follow up for "Unique Paths": N ...
- LeetCode: Unique Paths II 解题报告
Unique Paths II Total Accepted: 31019 Total Submissions: 110866My Submissions Question Solution Fol ...
- LEETCODE —— Unique Paths II [动态规划 Dynamic Programming]
唯一路径问题II Unique Paths II Follow up for "Unique Paths": Now consider if some obstacles are ...
- LEETCODE —— Unique Paths II [Dynamic Programming]
唯一路径问题II Unique Paths II Follow up for "Unique Paths": Now consider if some obstacles are ...
- [LeetCode] Unique Paths II 不同的路径之二
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
- Leetcode Unique Paths II
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
- [Leetcode] unique paths ii 独特路径
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
- [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 ...
- 动态规划小结 - 二维动态规划 - 时间复杂度 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) 有关 这种情况下,时间 ...
- [Leetcode Week12]Unique Paths II
Unique Paths II 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/unique-paths-ii/description/ Descrip ...
随机推荐
- Elasticsearch-2.3 (OpenLogic CentOS 7.2)
平台: CentOS 类型: 虚拟机镜像 软件包: elasticsearch-2.3 application server basic software big data elasticsearch ...
- Check-Point-Security-Gateway-BYOL-R77.30-041.161
平台: CentOS 类型: 虚拟机镜像 软件包: checkpoint redhat smartconsole basic software security vfw 服务优惠价: 按服务商许可协议 ...
- SqlServer Alwayson 搭建排错记录(一)
这几天搭建alwayson,碰到一堆问题,解决起来花了不少时间,特此记下几个有代表性的,以免以后再碰到做重复功. 一.创建可用性组 使用SSMS的创建可用性组向导,添加了一个主副本node1,一个辅助 ...
- Netweaver和CloudFoundry里的trace开关
Netweaver 事务码SU01的参数设置里,有很多关于trace(跟踪)的开关. FPTRACELEVEL: 设成04后,能将ADS生成PDF的trace信息写入生成的PDF. 详细原理参考我的b ...
- SAP成都研究院CEC团队三巨头之一:M君的文章预告
国人总倾向于把特点或者作用类似的人或物放在一起比较并做出排名,于是就有了许多"某某某三巨头"的称谓. 最举世闻名的莫过于二战三巨头:丘吉尔,罗斯福和斯大林. 还有陪伴咱八零后童年时 ...
- IOS UITabBarController(控制器)的子控制器
UITabBarController的简单使用 ● UITabBarController的使用步骤 ➢ 初始化UITabBarController ➢ 设置UIWindow的rootViewContr ...
- slenium的xpath几种定位方式
练习地址,以下面地址为例: http://www.w3school.com.cn/example/xmle/books.xml 1. 查找book对象 //book #所有的数 //book[1] ...
- 【洛谷2152】[SDOI2009] SuperGCD(Python好题)
点此看题面 大致题意: 给你两个长度\(\le10000\)的正整数,让你求它们的\(gcd\). Python 高精请绕道. 这题的正解应该是Python. 对于这种高精题,肯定是Python最方 ...
- fmt - 简易的文本格式优化工具 simple optimal text formatter
总览 (SYNOPSIS) ../src/fmt [-DIGITS] [OPTION]... [FILE]... 描述 (DESCRIPTION) 重新 格式化 文件 FILE(s) 中的 每一个 段 ...
- python剑指offer 链表中环的入口节点
题目: 一个链表中包含环,请找出该链表的环的入口结点. 思路: 先说个定理:两个指针一个fast.一个slow同时从一个链表的头部出发, fast一次走2步,slow一次走一步,如果该链表有环,两个指 ...