980. Unique Paths III
题目来源:
https://leetcode.com/problems/unique-paths-iii/
自我感觉难度/真实难度:
题意:
分析:
回溯法,直接DFS就可以了
自己的代码:
class Solution:
def uniquePathsIII(self, grid: List[List[int]]) -> int:
res=0
n=len(grid)
m=len(grid[0])
for i in range(n):
for j in range(m):
if grid[i][j]==1:
start_i,start_j=i,j
if grid[i][j]==2:
end_i,end_j=i,j
def dfs(grid,k,l):
if grid[k][l]==-1:
return
if grid[k][l]==2:
for i in range(n):
for j in range(m):
if grid[n][m]==0:
return
res+=1
return
grid[k][l]=-1
left,right,up,down=l-1,l+1,k-1,k+1
if left>=0:
dfs(grid,k,left)
if right<=m:
dfs(grid,k,right)
if up>=0:
dfs(grid,up,l)
if down<=n:
dfs(grid,down,l)
dfs(grid,start_i,start_j)
return res
代码效率/结果:
虽然有思路,但是写出来的代码细节通不过,注意退回去的时候
优秀代码:
代码效率/结果:
自己优化后的代码:
反思改进策略:
1.函数输入变量设计的时候,我们要的是输入每次不同的东西。如果是全局的东西,我们可以不当作输入变量
2.回溯法,如果需要改变全局变量的值,那么从DFS后面回去的时候,要改回来!!!
写题时间时长:
980. Unique Paths III的更多相关文章
- LC 980. Unique Paths III
On a 2-dimensional grid, there are 4 types of squares: 1 represents the starting square. There is e ...
- 原题链接在这里:980. Unique Paths III
原题链接在这里:https://leetcode.com/problems/unique-paths-iii/ 题目: On a 2-dimensional grid, there are 4 typ ...
- leetcode 980. Unique Paths III
On a 2-dimensional grid, there are 4 types of squares: 1 represents the starting square. There is e ...
- 【leetcode】980. Unique Paths III
题目如下: On a 2-dimensional grid, there are 4 types of squares: 1 represents the starting square. Ther ...
- 【LeetCode】980. Unique Paths III解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 回溯法 日期 题目地址:https://leetco ...
- Leetcode之深度优先搜索&回溯专题-980. 不同路径 III(Unique Paths III)
Leetcode之深度优先搜索&回溯专题-980. 不同路径 III(Unique Paths III) 深度优先搜索的解题详细介绍,点击 在二维网格 grid 上,有 4 种类型的方格: 1 ...
- [Swift]LeetCode980. 不同路径 III | Unique Paths III
On a 2-dimensional grid, there are 4 types of squares: 1 represents the starting square. There is e ...
- [LeetCode] Unique Paths II 不同的路径之二
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
- [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 ...
随机推荐
- sql or语法
1.mysql中or语法的使用,在mysql语法中or使用注意点. 项目遇到坑,遍历发放奖励数据查询错误!!! $sql = 'SELECT * FROM `vvt_spread_doubleegg_ ...
- 【canvas系列】canvas实现“ 简单的Amaziograph效果”--画对称图【强迫症福利】
标题很难引人入胜,先放个效果图好了 如果图片吸引不了你,那我觉得也就没啥看的了. demo链接: https://win7killer.github.io/demo_set/html_demo/can ...
- idea 导入Mapper错误报错设置
这个报错如图: 其实这个报错是错误,因为运行一切正常. 解决办法:
- geogebra几何画图工具用法
1. intersectPath :该命令可以自动“算出”对应多边形的交汇区域 2. 静态文本可以指定到一个对象的中间这样将来动态变化对象大小时也不出现问题 3.export worksheet 4. ...
- CentOS6.4 下安装 jdk1.7.0_67
1.卸载系统自带的jdk 1.1.查看该操作系统上是否已经安装了jdk [root@xhTest-1 ~]# rpm -qa | grep jdk 1.2.删除系统自带的jdk [root@xhTes ...
- vue-cli的webpack模板项目配置文件说明
如果没有vue-cli,那么进行vue项目的开发环境配置将是很费力的一件事情,现在脚手架可以快速构建一个开发环境,是很不错的.不过对于脚手架构建的配置,还是要大概了解一下,方便自己调试配置. 初始化一 ...
- Oracle EBS GL 创建会计科目
SELECT ct.trx_number ,l.accounting_class_code ,l.entered_dr ,l.entered_cr ,fnd_flex_ext.get_segs('SQ ...
- VC++下使用ADO操作数据库
VC++下使用ADO操作数据库主要要用到 _ConnectionPtr,_CommandPtr,_RecordsetPtr三个ADO对象指针,我查找了相关资料,发现网上源码很多,但是都相对凌乱,于是自 ...
- 使用UICollectionView
使用UICollectionView 使用UICollectionView的流程: 1. 设定一个UICollectionViewFlowLayout 2. 使用这个设定的UICollectionVi ...
- 使用yii AR 完成单个表的CURD操作
什么是AR(ActiveRecord) Active Record (活动记录,以下简称AR)提供了一个面向对象的接口, 用以访问数据库中的数据.一个 AR 类关联一张数据表, 每个 AR 对象对应表 ...