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 ...
随机推荐
- Code Signal_练习题_Minesweeper
In the popular Minesweeper game you have a board with some mines and those cells that don't contain ...
- springboot项目的重定向和转发
下面是idea软件创建的项目目录,这里总结了一下转发与重定向的问题,详解如下. 首先解释一下每个文件夹的作用,如果你是用的是idea创建的springboot项目,会在项目创建的一开始resource ...
- 计算mysql中某个字段某字符出现的次数,case when 和 截取字符的用法
select LENGTH(type) - LENGTH(replace(type,'_','')) as counts from sa_log_olap where type like 'XX_XX ...
- java运算符优先级别
算数-->关系-->逻辑-->赋值
- web容器向jar中寻找资源
示例代码下载地址 https://files.cnblogs.com/files/Mike_Chang/web%E5%AE%B9%E5%99%A8%E5%90%91jar%E4%B8%AD%E5%AF ...
- Pig类型转换
users.data的内容如下: lisg 28 75 dengsl 24 88 强制类型转换 users = load '/users.data' fehed = foreach users gen ...
- 外业数据采集平台(GPS+Android Studio+Arcgis for android 100.2.1)
外业数据采集平台 1. 综述 在室外,通过平板或者手机接收GPS坐标,实时绘制点.线.面数据,以便为后续进行海域监测.土地确权.地图绘图提供有效数据和依据. 2. 技术路线 Android studi ...
- 深入理解SVG坐标体系和transformations- viewport, viewBox,preserveAspectRatio
本文翻译自blog: https://www.sarasoueidan.com/blog/svg-coordinate-systems/ SVG元素不像其他HTML元素一样受css盒子模型所制约.这个 ...
- 关于3d打印
3d打印技术是20世纪90年代逐渐兴起的一项先进的制造技术.3D打印——three-dimensional printing,简称3dp.又称三维打印或立体打印,最早由美国麻省理工学院于1993年开发 ...
- Jenkins操作,实现增删改查
Jenkins的版本是:Jenkins2.138.1 实现的操作接口: using System; using System.Collections.Generic; namespace iHRPub ...