[LeetCode]题解(python):063-Unique path II
题目来源
https://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.
题意分析
Input:a matrix as list[list[int]]
Output:the number of path
Conditions:从左上角走到左下角,注意有障碍不能通过
题目思路
上一题是用数学方法直接解,这一题用动态规划吧,因为每一步都会基于上一格或者是左一格,动态方程为dp[i][j] = dp[i][j-1] + dp[i-1][j]
注意初始化的时候如果遇到障碍,后面的位置不能初始化为0了
AC代码(Python)
class Solution(object):
def uniquePathsWithObstacles(self, obstacleGrid):
"""
:type obstacleGrid: List[List[int]]
:rtype: int
"""
m = len(obstacleGrid)
n = len(obstacleGrid[0])
dp = [[0 for i in range(n)] for j in range(m)]
for i in range(n):
if obstacleGrid[0][i] == 1:
break
else:
dp[0][i] = 1 for i in range(m):
if obstacleGrid[i][0] == 1:
break
else:
dp[i][0] = 1 for i in range(1, m):
for j in range(1, n):
if obstacleGrid[i][j] == 0:
dp[i][j] = dp[i - 1][j] + dp[i][j - 1] return dp[m - 1][n - 1]
[LeetCode]题解(python):063-Unique path II的更多相关文章
- LeetCode 63. Unique Path II(所有不同路径之二)
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
- leetcode63—Unique Path II
A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...
- 【LeetCode】063. Unique Paths II
题目: Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. H ...
- Java for LeetCode 063 Unique Paths II
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
- Unique path ii
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
- [Leetcode 62]机器人走路Unique Path 动态规划
[题目] A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below) ...
- LeetCode 题解 Search a 2D Matrix II。巧妙!
[ [1, 4, 7, 11, 15], [2, 5, 8, 12, 19], [3, 6, 9, 16, 22], [10, 13, 14, 17, 24], [18, 21, 23, 26, 30 ...
- 063 Unique Paths II 不同路径 II
这是“不同路径” 的进阶问题:现在考虑网格中有障碍物.那样将会有多少条不同的路径从左上角到右下角?网格中的障碍物和空位置分别用 1 和 0 来表示.例如,如下所示在 3x3 的网格中有一个障碍物.[ ...
- LeetCode(63)Unique Paths II
题目 Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. Ho ...
- LeetCode题解之Pascal's Triangle II
1.题目描述 2.题目分析 题目要求返回杨辉三角的某一行,需要将杨辉三角的某行的全部计算出来. 3.代码实现 vector<int> getRow(int rowIndex) { ) ,) ...
随机推荐
- BZOJ3658 : Jabberwocky
考虑将某线段下方的点取走: 将所有点从低到高排序 每扫描到一条水平线,对于上面每个点,找到它下面同色的前驱后继,统计中间点的个数 然后再把线上所有点插入数据结构中 最后再统计相邻的同色的点之间的点个数 ...
- (转)js:字符串(string)转json
第一种方式: 使用js函数eval(); testJson=eval(testJson);是错误的转换方式. 正确的转换方式需要加(): testJson = eval("(" + ...
- c# 使用GetOleDbSchemaTable获取access数据库结构
c# 使用GetOleDbSchemaTable获取access数据库结构 ado.net可以使用GetOleDbSchemaTable方法来获取access数据库的结构,但得到的datatable的 ...
- OpenCV 2.4.10 Linux Qt Conifguration
Download CMake 2.8.12 Extract the file, and run "./bootstrap", then "make", then ...
- Network | TCP congestion control
拥塞控制算法:1. 加性增.乘性减:2. 慢启动:3. 对超时事件作出反应: 整体过程如下: 慢启动->到达阈值->加性增(窗口+1个MSS), 这个阶段叫拥塞避免(CA)->3个冗 ...
- 用Get-ADComputer取非常用属性的值
由于GE使用的是Windows2003+Powershell2.0, 所以某些命令无法使用,比如想取lastLogon和lastLogonTimestamp这两个属性,在Powershell3.0下可 ...
- 分布式架构高可用架构篇_05_fastdfs集群的安装
参考: 龙果学院http://www.roncoo.com/share.html?hamc=hLPG8QsaaWVOl2Z76wpJHp3JBbZZF%2Bywm5vEfPp9LbLkAjAnB%2B ...
- centos fastdfs 多服务器 多硬盘 多组 配置详解
说正文前,先感谢happy_fish100提供的fastdfs,轻量级的分布式文件服务器. 随着用户量的变大,图片,视频等的量会不断的增大,这个时候一个硬盘可能不够用了,就要加硬盘.硬盘加不了时,就要 ...
- ORA-12518: TNS: 监听程序无法分发客户机连接
在团队成员增多时,经常出现“无法分发客户端连接”等问题.在网上搜索一番后,最终解决了该问题,现将解决方案总结如下,以供参考和以后备用. 原因:团队成员增多,原有数据库设置不够用,导致连接plsql和启 ...
- Mininet实验 使用l2_multi模块寻找最短路径实验
参考:使用l2_multi模块寻找最短路径实验 1. 实验目的 1.认识VND并且掌握其基本使用方法. 2.学会使用pox控制器的l2_multi模块寻找主机间的最短传输路径. 2. 实验原理 VND ...