LeetCode 79 单词搜索【c++/java详细题解

「这是我参与11月更文挑战的第25天,活动详情查看:2021最后一次更文挑战」。 来源于湖科大教书匠

1、题目

给定一个 m x n 二维字符网格 board 和一个字符串单词 word 。如果 word 存在于网格中,返回 true ;否则,返回 false

单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中“相邻”单元格是那些水平相邻或垂直相邻的单元格。同一个单元格内的字母不允许被重复使用。

示例 1:

1
2
arduino复制代码输入:board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "ABCCED"
输出:true

示例 2:

1
2
arduino复制代码输入:board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "SEE"
输出:true

示例 3:

1
2
arduino复制代码输入:board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "ABCB"
输出:false

提示:

  • m == board.length
  • n = board[i].length
  • 1 <= m, n <= 6
  • 1 <= word.length <= 15
  • boardword 仅由大小写英文字母组成

2、思路

(回溯) O(n23k)O(n^2 3^k)O(n23k)

深度优先搜索,我们定义这样一种搜索顺序,即先枚举单词的起点,然后依次枚举单词的每个字母。在这个过程中需要将已经使用过的字母改成一个特殊字母,以避免重复使用字符。


递归函数设计:

1
c复制代码bool dfs(vector<vector<char>>& board, string& word,int u,int x,int y)

u代表当前枚举到了目标单词wordu个位置。

xy是当前搜索到的二维字符网格的横纵坐标。

搜索过程如下:

  • 1、在二维字符网格中枚举每个单词的起点。
  • 2、从该起点出发向四周搜索单词word,并记录此时枚举到单词word的第u个位置 ( u0开始)。
  • 3、如果当前搜索的位置(x,y)的元素board[x][y] == word[u],则继续向四周搜索。
  • 4、直到枚举到单词word的最后一个字母返回ture,否则返回false

递归边界:

  • 1、当搜索过程出现当前位置board[x][y] != word[u] ,说明当前路径不合法,返回false
  • 2、u == word.size() - 1,成功搜索到单词末尾,返回true

实现细节:

  • 1、搜索过的位置继续搜索下一层时,需要对当前位置进行标识,表示已经搜索
  • 2、可以使用偏移数组来简化代码。


时间复杂度分析: 单词起点一共有 n2n^2n2 个,单词的每个字母一共有上下左右四个方向可以选择,但由于不能走回头路,所以除了单词首字母外,仅有三种选择。所以总时间复杂度是 O(n23k)O(n^2 3^k)O(n23k) 。

3、c++代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
c复制代码class Solution {
public:
bool exist(vector<vector<char>>& board, string word) {
for(int i = 0; i < board.size(); i++)
for(int j = 0; j < board[i].size(); j++)
if(dfs(board,word,0,i,j)) return true;
return false;
}
int dx[4] = {-1,0,1,0}, dy[4] = {0,1,0,-1}; //方向数组
bool dfs(vector<vector<char>>& board, string& word,int u,int x,int y)
{
if(board[x][y] != word[u]) return false;
if(u == word.size() - 1) return true;
char t = board[x][y];
board[x][y] = '.';
for(int i = 0; i < 4; i++)
{
int a = x + dx[i], b = y + dy[i];
//出界或者走到已经搜索过的位置
if(a < 0 || a >= board.size() || b < 0 || b >= board[0].size() || board[a][b] == '.') continue;
if(dfs(board,word,u+1,a,b)) return true;
}
board[x][y] = t;
return false;
}
};

4、java代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
java复制代码class Solution {
public boolean exist(char[][] board, String word) {
for(int i = 0; i < board.length; i++)
for(int j = 0; j < board[i].length; j++)
if(dfs(board,word,0,i,j)) return true;
return false;
}
int[] dx = new int[]{-1,0,1,0}, dy = new int[]{0,1,0,-1};
boolean dfs(char[][] board, String word,int u,int x,int y)
{
if(board[x][y] != word.charAt(u)) return false;
if(u == word.length() - 1) return true;
char t = board[x][y];
board[x][y] = '.';
for(int i = 0; i < 4; i++)
{
int a = x + dx[i], b = y + dy[i];
if(a < 0 || a >= board.length|| b < 0 || b >= board[0].length || board[a][b] == '.') continue;
if(dfs(board,word,u+1,a,b)) return true;
}
board[x][y] = t;
return false;
}
}

原题链接:79. 单词搜索
在这里插入图片描述

本文转载自: 掘金

开发者博客 – 和开发相关的 这里全都有

0%