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 ...
随机推荐
- Jquery使用Id获取焦点和失去焦点
Jquery使用Id获取焦点和失去焦点有2种方法 先用第一种(val()=="空"): <div> <input type="text" id ...
- 使用AJAX实现分页
Fenye.html <!DOCTYPE html> <html> <head> <title>分页</title> </head&g ...
- jQuery源码学习笔记一
学习jQuery源码,我主要是通过妙味视频上学习的.这里将所有的源码分析,还有一些自己弄懂过程中的方法及示例整理出来,供大家参考. 我用的jquery v2.0.3版本. var rootjQuery ...
- drupal7设置不含www的url跳转到含www的url
打开drupal的.htaccess文件 找到 If your site can be accessed both with and without the 'www.' prefix 将下面对应的三 ...
- UEditor百度网页编辑器JSP版配置与调试
最近用公司后台时发现那编辑器还难用,就想着给它换一个.在网上找了下资料也对比了一下,后决定用百度的UEditor. 在UEditor官网下载了一个开发版1.4.3.3Jsp 版本UTF-8编码的压缩包 ...
- 【java错误】System.out.println()出错
今天想测试java的System的类,没想到居然出错了.在同一个包下的java文件System全错,而其他包中的System没错.上网查了下资料,原来我是重定义了System类,覆盖了原来的Syste ...
- React—Native开发之原生模块向JavaScript发送事件
首先,由RN中文网关于原生模块(Android)的介绍可以看到,RN前端与原生模块之 间通信,主要有三种方法: (1)使用回调函数Callback,它提供了一个函数来把返回值传回给JavaScript ...
- 9 tensorflow提示in different while loops的错误该如何解决
示例代码 ii=tf.constant(0,dtype=tf.int32) loop__cond=lambda a: tf.less(a,sentence_length) loop__vars=[ii ...
- 《SQL Server 2008从入门到精通》--20180704
XML查询技术 XML文档以一个纯文本的形式存在,主要用于数据存储.不但方便用户读取和使用,而且使修改和维护变得更容易. XML数据类型 XML是SQL Server中内置的数据类型,可用于SQL语句 ...
- MySQL binlog格式解析
MySQL binlog格式解析 binlog想必大家都不陌生,在主从复制或者某些情况下的数据恢复会用到.由于binlog是二进制数据,要查看一般都借助mysqlbinlog工具.这篇笔记分析了b ...