[leetcode]Unique Paths II @ Python
原题地址:https://oj.leetcode.com/problems/unique-paths-ii/
题意:
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.
解题思路:这道题是设置了障碍的,也是用动态规划解决。
代码:
class Solution:
# @param obstacleGrid, a list of lists of integers
# @return an integer
def uniquePathsWithObstacles(self, obstacleGrid):
m = len(obstacleGrid); n = len(obstacleGrid[0])
res = [[0 for i in range(n)] for j in range(m)]
for i in range(m):
if obstacleGrid[i][0] == 0:
res[i][0] = 1
else:
res[i][0] == 0
break
for i in range(n):
if obstacleGrid[0][i] == 0:
res[0][i] = 1
else:
res[0][i] = 0
break
for i in range(1, m):
for j in range(1, n):
if obstacleGrid[i][j] == 1: res[i][j] = 0
else:
res[i][j] = res[i-1][j] + res[i][j-1]
return res[m-1][n-1]
[leetcode]Unique Paths II @ Python的更多相关文章
- 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 ...
 
随机推荐
- php 中使用include、require、include_once、require_once的区别
			
在PHP中,我们经常会通过include.require.include_once.require_once来引用文件,都可以达到引用文件的目的,但他们之间又有哪些区别呢,接一下我们详细的介绍一下 i ...
 - Linux VMware tools安装步骤
			
Linux VMware tools安装步骤: 1.安装环境介绍 #虚拟机版本:VMware-workstation-full-10 #linux分发版本:CentOS-6.4-i386-LiveCD ...
 - tomcat 输入学习
			
Tomcat学习—Tomcat7 修改/webapps/ROOT发布路径(Linux和windows环境) https://blog.csdn.net/u010648555/article/detai ...
 - MockMvc 对 Spring Boot 进行单元测试
			
import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.ann ...
 - day16--HTML、CSS、JavaScript总结
			
HTML 一大堆的标签:块级.行内 CSS position background text-align padding font-size background-image z-index ...
 - 乐观锁和悲观锁及CAS实现
			
乐观锁与悲观锁 悲观锁:总是假设最坏的情况,每次去拿数据的时候都认为别人会修改,所以每次在拿数据的时候都会上锁,这样别人想拿这个数据就会阻塞直到它拿到锁.传统的关系型数据库里边就用到了很多这种锁机制, ...
 - mysql数据库备份 mysqldump
			
一.--all-databases /application/mysql3307/bin/mysqldump -uroot -S /application/mysql3307/logs/mysql.s ...
 - Python int 中 add abs 方法
			
1+1 实际等于1.__add__(1) __abs__ 取绝对值
 - 用HTML+CSS画出一个同心圆
			
参加web前端校招的同学们经常会遇到这样的面试题:用HTML+CSS画出一个同心圆. 例如: 这道题主要考验的是基础盒模型布局能力和倒圆角属性的巧用. 1.html代码 <body> &l ...
 - 061 hive中的三种join与数据倾斜
			
一:hive中的三种join 1.map join 应用场景:小表join大表 一:设置mapjoin的方式: )如果有一张表是小表,小表将自动执行map join. 默认是true. <pro ...