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 ...
随机推荐
- xamarin.Android ImageView 异步加载网络图片
/// <summary> /// 异步获取文件流 /// </summary> /// <param name="url"></para ...
- 14:求10000以内n的阶乘
14:求10000以内n的阶乘 查看 提交 统计 提问 总时间限制: 5000ms 内存限制: 655360kB 描述 求10000以内n的阶乘. 输入 只有一行输入,整数n(0<=n< ...
- 全功能开发团队(FSD)
- Android EditText方框验证码 短信验证码攻略
本文由xiawe_i提供. xiawe_i的博客地址是: http://www.jianshu.com/u/fa9f03a240c6 项目中有这样一个需求: 验证码页是四个方框,输入验证码方框颜色改变 ...
- web项目启动时,自动执行代码的几种方式
在项目开发过程中,往往需要一些功能随着项目启动而优先启动,下面我总结几种方式(非spring boot) spring boot的参考 spring boot 学习之路9 (项目启动后就执行特定方法) ...
- SQLSERVER性能计数器的简单剖析
SQLSERVER性能计数器的简单剖析 今晚看了这篇文章:SQL Server 2012新performance counter:非常实用的Batch Resp Statistics 文章里介绍到SQ ...
- 使用 Azure CLI 创建和管理 Linux VM
Azure 虚拟机提供完全可配置的灵活计算环境. 本教程介绍 Azure 虚拟机的基本部署项目,例如选择 VM 大小.选择 VM 映像和部署 VM. 你将学习如何执行以下操作: 创建并连接到 VM 选 ...
- Jenkins操作,实现增删改查
Jenkins的版本是:Jenkins2.138.1 实现的操作接口: using System; using System.Collections.Generic; namespace iHRPub ...
- 解决 There are no resources that can be added or removed from the server
网上下载了一个项目,在eclipse中部署时,加载项目到tomcat中项目名称无法显示,报出There are no resources that can be added or removed fr ...
- .net验证是否合法邮箱和ip地址的方式
通常情况下第一时间会想到使用正则表达式去验证,但由于正则表达式过于复杂或者没有考虑到某些情况,从而导致问题或者判断的效率低.下面通过另一种方式去判断. 判断是否合法邮箱: /// <summar ...