[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 ...
随机推荐
- 缓存之EHCache(一)
源文: http://blog.csdn.net/l271640625/article/details/20528573 一.简介 非常简单,而且易用. ehcache 是一个非常轻量级的缓存 ...
- css 文件连接不到网页
css 文件连接不到网页 编码错误,将编码改为utf-8 Rom后正常
- python 全栈开发,Day97(Token 认证的来龙去脉,DRF认证,DRF权限,DRF节流)
昨日内容回顾 1. 五个葫芦娃和三行代码 APIView(views.View) 1. 封装了Django的request - request.query_params --> 取URL中的参数 ...
- hdu 1455 N个短木棒 拼成长度相等的几根长木棒 (DFS)
N根短木棒 能够拼成几根长度相等的长木棒 求长木棒的长度 如果答案不止一种 输出最小的 Sample Input95 2 1 5 2 1 5 2 141 2 3 40 Sample Output65 ...
- Java httpClient 发送http请求
RestTemplate ObjectMapper将string反序列化为WeatherResponse类 RestTemplate通过spring配置注入
- thinkphp自定义分页类
先来看下这个分页的样式,没写css,确实丑 什么时候写样式再来上传下css吧...... 就是多一个页面跳转功能 先把这个代码贴一下 <?php namespace Component; cla ...
- la 4394
题解: 区间dp 令f[i][j]表示搞好i-j的最小值 首先如果不用涂色 那么可以从f[i][k] f[k+1][j]转移 如果要涂色,那么就从f[i][k][a](表示i-k全为a)+f[k+1] ...
- CSS 2. 盒模型|浮动
1.盒模型 盒模型: 在网页中 基本上都会显示一些方方正正的盒子,这种盒子就被我们称为盒模型.重要的属性: width,height,padding,border, margin 盒子模型通过四个边界 ...
- 《Gradle权威指南》--Android Gradle插件
No1: Android Gradle插件分类 App插件id:com.android.application Library插件id:com.android.library Test插件id:com ...
- hdu 2546 饭卡【01背包】
题目链接:https://vjudge.net/contest/103424#problem/C 饭卡 Time Limit: 5000/ ...