[leetcode DP]63. 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
.
障碍物对应的dp表中设为0即可,为了使代码简洁,多加了两行dp[i][0]和dp[0][j]
class Solution(object):
def uniquePathsWithObstacles(self, obstacleGrid):
m,n = len(obstacleGrid),len(obstacleGrid[0])
dp = [[0 for j in range(n+1)] for i in range(m+1)]
dp[0][1] = 1
for i in range(1,m+1):
for j in range(1,n+1):
if not obstacleGrid[i-1][j-1]:
dp[i][j] = dp[i][j-1] + dp[i-1][j]
return dp[m][n]
还有一种占用O(N)空间的方法,感觉挺不错的
思路:用一个数组tmp记录每一行中每一个位置拥有的次数,每到一个没有障碍物的位置,那么这个位置自动继承上一个位置上所拥有的次数
class Solution(object):
def uniquePathsWithObstacles(self, obstacleGrid):
m,n=len(obstacleGrid),len(obstacleGrid[0])
tmp = [0]*(n+1)
tmp [n-1] = 1
for i in range(m-1,-1,-1):
for j in range(n-1,-1,-1):
if obstacleGrid[i][j]:
tmp[j] = 0
else:
tmp[j] += tmp [j+1]
return tmp[0]
[leetcode DP]63. Unique Paths II的更多相关文章
- 【一天一道LeetCode】#63. Unique Paths II
一天一道LeetCode (一)题目 Follow up for "Unique Paths": Now consider if some obstacles are added ...
- 【LeetCode】63. Unique Paths II
Unique Paths II Follow up for "Unique Paths": Now consider if some obstacles are added to ...
- 【LeetCode】63. Unique Paths II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/unique-pa ...
- LeetCode OJ 63. Unique Paths II
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
- leetcode@ [62/63] Unique Paths II
class Solution { public: int uniquePathsWithObstacles(vector<vector<int>>& obstacleG ...
- leetcode 62. Unique Paths 、63. Unique Paths II
62. Unique Paths class Solution { public: int uniquePaths(int m, int n) { || n <= ) ; vector<v ...
- 62. Unique Paths && 63 Unique Paths II
https://leetcode.com/problems/unique-paths/ 这道题,不利用动态规划基本上规模变大会运行超时,下面自己写得这段代码,直接暴力破解,只能应付小规模的情形,当23 ...
- 【LeetCode练习题】Unique Paths II
Unique Paths II Follow up for "Unique Paths": Now consider if some obstacles are added to ...
- [LeetCode] 63. Unique Paths II 不同的路径之二
A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...
随机推荐
- POJ 3255 Roadblocks (次短路 SPFA )
题目链接 Description Bessie has moved to a small farm and sometimes enjoys returning to visit one of her ...
- 利用Object.defineProperty 简化 Chrome插件本地存储操作
通常谷歌插件本地存储写法很别扭☹,如 chrome.storage.sync.get(null,function(data){ //todo console.log(data); }); 如果get ...
- CSS权重的问题
important > 内联 > ID > 类 > 标签 | 伪类 | 属性选择 > 伪对象 > 继承 > 通配符 1.行内样式,指的是html文档中定义的s ...
- Spring4笔记7--AspectJ 对 AOP 的实现
AspectJ 对 AOP 的实现: 对于 AOP 这种编程思想,很多框架都进行了实现.Spring 就是其中之一,可以完成面向切面编程.然而,AspectJ 也实现了 AOP 的功能,且其实现方式更 ...
- 可能是最漂亮的Spring事务管理详解
Java面试通关手册(Java学习指南):https://github.com/Snailclimb/Java_Guide 微信阅读地址链接:可能是最漂亮的Spring事务管理详解 事务概念回顾 什么 ...
- ubuntu更新源列表
1. 备份源列表 sudo cp /etc/apt/sources.list /etc/apt/sources.list_backup 2.修改更新源 打开源列表 sudo gedit /etc/ap ...
- Flask:redirect()函数
Windows 10家庭中文版,Python 3.6.4,Flask 1.0.2 重定向,就是在客户端提交请求后,本来是访问A页面,结果,后台给了B页面,当然,B页面中才有需要的信息. 在Flask中 ...
- MySQL字符集编码相关
Windows 10家庭中文版,MySQL 5.7.20,2018-05-07 Part.1 查找数据库的字符集编码 查看MySQL字符集编码:status命令 使用命令行登录MySQL服务器,然后 ...
- mac下---charles抓包https
网上找的很多安装包都有问题,终于找到个可用的! 下载地址: http://pan.baidu.com/s/1pLAONbX ———————————————————————————— 教程转载:htt ...
- opencv之dft及mat类型转换
跑实验时用到dft这个函数,根据教程,需要先将其扩充到最优尺寸,但我用逆变换后发现得到的mat的维数竟然不一样.因此还是不要扩展尺寸了. 参考:http://www.xpc-yx.com/2014/1 ...