leet笔记-63.不同路径II
时间:2022-07-24
本文章向大家介绍leet笔记-63.不同路径II,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
leet笔记-63.不同路径II
题目
思路:
【参考】leetcode官方解释
关键点1:只能向右或者向下
关键点2:有障碍物为1, 无障碍物为0
根据 关键点1 和 关键点2 来确定动态规划的步骤:
- 定义DP数组 可以直接利用题目中的obstacleGrid
-
找出关系数组元素间的关系式
初始化好之后,发现动态方程:
遍历时:
- 若当前为0。那么,直接计算动态方程下的计算过程
- 若当前不为0。那么,直接置该位置的值为0
- 找出初始值 利用动态规划解题的时候,初始化首行和首列的时候,障碍物1后面的都不可达
那么,按照以上点给出代码
class Solution(object):
# 时间复杂度: O(mxn)
# 空间复杂度: O(mxn)
def uniquePathsWithObstacles1(self, obstacleGrid):
# row number
m = len(obstacleGrid)
# column number
n = len(obstacleGrid[0])
# If the starting cell has an obstacle, then simply return as there would be
# no paths to the destination.
# so, give the result for 0
if obstacleGrid[0][0] == 1:
return 0
# Number of ways of reaching the starting cell = 1.
obstacleGrid[0][0] = 1
# Filling the values for the first column
for clo in range(1, m):
obstacleGrid[clo][0] = int(obstacleGrid[clo][0] == 0 and obstacleGrid[clo-1][0] == 1)
# Filling the values for the first row
for row in range(1, n):
obstacleGrid[0][row] = int(obstacleGrid[0][row] == 0 and obstacleGrid[0][row-1] == 1)
# start from the cell[1][1] fill up the values
# DP way of reach: cell[i][j] = cell[i-1][j] + cell[i][j-1]
for i in range(1, m):
for j in range(1, n):
if obstacleGrid[i][j] == 0:
obstacleGrid[i][j] = obstacleGrid[i-1][j] + obstacleGrid[i][j-1]
else:
obstacleGrid[i][j] = 0
return obstacleGrid[m-1][n-1]
时间复杂度: O(mxn) 遍历obstacleGrid的每一个格子,所需要的时间度量为m*n
空间复杂度: O(1), 没有产生额外的数据存放空间
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- 小加载动画
- 日志收集工具简单对比
- [蓝桥杯][2013年第四届真题]幸运数
- 04 . Filebeat简介原理及配置文件和一些案例
- 05 . ELK Stack+Redis日志收集平台
- python开发【第一篇】
- 内置函数--bin() oct() int() hex()
- 08 . Prometheus+Grafana监控haproxy+rabbitmq
- 内置函数值 -- chr() ord() -- 字符和ascii的转换
- python内置函数-compile()
- 02 . Shell变量和逻辑判断及循环使用
- Python内置函数(21)——filter
- 内置函数 -- filter 和 map
- 内置函数--global() 和 local()
- python file文件操作--内置对象open