Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 6b7dbee

Browse files
add LeetCode 79. 单词搜索
1 parent 1757a50 commit 6b7dbee

File tree

1 file changed

+99
-0
lines changed

1 file changed

+99
-0
lines changed
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
![](https://imgconvert.csdnimg.cn/aHR0cHM6Ly9jZG4uanNkZWxpdnIubmV0L2doL2Nob2NvbGF0ZTE5OTkvY2RuL2ltZy8yMDIwMDgyODE0NTUyMS5qcGc?x-oss-process=image/format,png)
2+
>仰望星空的人,不应该被嘲笑
3+
4+
## 题目描述
5+
给定一个二维网格和一个单词,找出该单词是否存在于网格中。
6+
7+
单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中"相邻"单元格是那些水平相邻或垂直相邻的单元格。同一个单元格内的字母不允许被重复使用。
8+
9+
10+
11+
示例:
12+
13+
```javascript
14+
board =
15+
[
16+
['A','B','C','E'],
17+
['S','F','C','S'],
18+
['A','D','E','E']
19+
]
20+
21+
给定 word = "ABCCED", 返回 true
22+
给定 word = "SEE", 返回 true
23+
给定 word = "ABCB", 返回 false
24+
```
25+
26+
提示:
27+
28+
```javascript
29+
board 和 word 中只包含大写和小写英文字母。
30+
1 <= board.length <= 200
31+
1 <= board[i].length <= 200
32+
1 <= word.length <= 10^3
33+
```
34+
35+
来源:力扣(LeetCode)
36+
链接:https://leetcode-cn.com/problems/word-search
37+
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
38+
39+
40+
## 解题思路
41+
上一期做了单词搜索2 `hard` 版本之后,这道题也想用字典树玩一玩,没想到超时了,后面一想,数据确实有点大,而且对于一个单词来说,建立一颗字典树岂不是很浪费,还要花时间码代码...
42+
43+
本题也是回溯的思路,不过期间做了一点小优化,还是通过动态更改当前所走的格子,省去了那份 开辟`vis` 数组的空间。
44+
45+
对于递归层次,由于最后一次计算时,层次多算了一次(即多加了一次),所以条件为 `>`
46+
47+
![](https://img-blog.csdnimg.cn/20200913183820425.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MjQyOTcxOA==,size_16,color_FFFFFF,t_70#pic_center)
48+
49+
50+
51+
```javascript
52+
var exist = function(grid, word) {
53+
let dfs = (x,y,t) => {
54+
// 最后一次还会 +1 因此,条件是大于
55+
if(t > word.length-1){
56+
return true
57+
}
58+
// 剪枝条件
59+
if(x<0 || x>=grid.length || y<0 || y>=grid[0].length || grid[x][y]!= word[t] || grid[x][y] == '#') return false
60+
let tmp = grid[x][y]
61+
// 开始走
62+
grid[x][y] = '#'
63+
// 从四个方向搜索,只要一个方向搜索有结果,那么直接返回 true即可
64+
let res = dfs(x+1,y,t+1) || dfs(x,y+1,t+1) || dfs(x-1,y,t+1) || dfs(x,y-1,t+1)
65+
if(res) return true
66+
// 回溯(重置)
67+
grid[x][y] = tmp
68+
return false
69+
}
70+
for(let i=0;i<grid.length;i++){
71+
for(let j=0;j<grid[0].length;j++){
72+
if(grid[i][j] == word[0]){
73+
let res = dfs(i,j,0)
74+
if(res) return true
75+
}
76+
}
77+
}
78+
return false
79+
};
80+
```
81+
82+
83+
## 最后
84+
文章产出不易,还望各位小伙伴们支持一波!
85+
86+
往期精选:
87+
88+
<a href="https://github.com/Chocolate1999/Front-end-learning-to-organize-notes">小狮子前端の笔记仓库</a>
89+
90+
<a href="https://yangchaoyi.vip/">访问超逸の博客</a>,方便小伙伴阅读玩耍~
91+
92+
![](https://img-blog.csdnimg.cn/2020090211491121.png#pic_center)
93+
94+
```javascript
95+
学如逆水行舟,不进则退
96+
```
97+
98+
99+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /