[LeetCode] Word Search 词语搜索
Given a 2D board and a word, find if the word exists in the grid.
The word can be constructed from letters of sequentially adjacent cell, where "adjacent" cells are those horizontally or vertically neighboring. The same letter cell may not be used more than once.
For example,
Given board =
- [
- ["ABCE"],
- ["SFCS"],
- ["ADEE"]
- ]
word = "ABCCED"
, -> returns true
,
word = "SEE"
, -> returns true
,
word = "ABCB"
, -> returns false
.
这道题是典型的深度优先遍历 DFS 的应用,原二维数组就像是一个迷宫,可以上下左右四个方向行走,我们以二维数组中每一个数都作为起点和给定字符串做匹配,我们还需要一个和原数组等大小的 visited 数组,是 bool 型的,用来记录当前位置是否已经被访问过,因为题目要求一个 cell 只能被访问一次。如果二维数组 board 的当前字符和目标字符串 word 对应的字符相等,则对其上下左右四个邻字符分别调用 DFS 的递归函数,只要有一个返回 true,那么就表示可以找到对应的字符串,否则就不能找到,具体看代码实现如下:
解法一:
- class Solution {
- public:
- bool exist(vector<vector<char>>& board, string word) {
- if (board.empty() || board[].empty()) return false;
- int m = board.size(), n = board[].size();
- vector<vector<bool>> visited(m, vector<bool>(n));
- for (int i = ; i < m; ++i) {
- for (int j = ; j < n; ++j) {
- if (search(board, word, , i, j, visited)) return true;
- }
- }
- return false;
- }
- bool search(vector<vector<char>>& board, string word, int idx, int i, int j, vector<vector<bool>>& visited) {
- if (idx == word.size()) return true;
- int m = board.size(), n = board[].size();
- if (i < || j < || i >= m || j >= n || visited[i][j] || board[i][j] != word[idx]) return false;
- visited[i][j] = true;
- bool res = search(board, word, idx + , i - , j, visited)
- || search(board, word, idx + , i + , j, visited)
- || search(board, word, idx + , i, j - , visited)
- || search(board, word, idx + , i, j + , visited);
- visited[i][j] = false;
- return res;
- }
- };
我们还可以不用 visited 数组,直接对 board 数组进行修改,将其遍历过的位置改为井号,记得递归调用完后需要恢复之前的状态,参见代码如下:
解法二:
- class Solution {
- public:
- bool exist(vector<vector<char>>& board, string word) {
- if (board.empty() || board[].empty()) return false;
- int m = board.size(), n = board[].size();
- for (int i = ; i < m; ++i) {
- for (int j = ; j < n; ++j) {
- if (search(board, word, , i, j)) return true;
- }
- }
- return false;
- }
- bool search(vector<vector<char>>& board, string word, int idx, int i, int j) {
- if (idx == word.size()) return true;
- int m = board.size(), n = board[].size();
- if (i < || j < || i >= m || j >= n || board[i][j] != word[idx]) return false;
- char c = board[i][j];
- board[i][j] = '#';
- bool res = search(board, word, idx + , i - , j)
- || search(board, word, idx + , i + , j)
- || search(board, word, idx + , i, j - )
- || search(board, word, idx + , i, j + );
- board[i][j] = c;
- return res;
- }
- };
Github 同步地址:
https://github.com/grandyang/leetcode/issues/79
类似题目:
参考资料:
https://leetcode.com/problems/word-search/
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] Word Search 词语搜索的更多相关文章
- [LeetCode] 79. Word Search 词语搜索
Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from l ...
- [LeetCode] Word Search II 词语搜索之二
Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...
- [LeetCode] 79. Word Search 单词搜索
Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from l ...
- Leetcode: word search
July 6, 2015 Problem statement: Word Search Given a 2D board and a word, find if the word exists in ...
- LeetCode: Word Search 解题报告
Word SearchGiven a 2D board and a word, find if the word exists in the grid. The word can be constru ...
- [LeetCode OJ] Word Search 深度优先搜索DFS
Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from l ...
- LeetCode 79. Word Search单词搜索 (C++)
题目: Given a 2D board and a word, find if the word exists in the grid. The word can be constructed fr ...
- [leetcode]Word Search @ Python
原题地址:https://oj.leetcode.com/problems/word-search/ 题意: Given a 2D board and a word, find if the word ...
- [Leetcode] word search 单词查询
Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from l ...
随机推荐
- 史上最全、JavaScript基础篇
本章内容: 简介 定义 注释 引入文件 变量 运算符 算术运算符 比较运算符 逻辑运算符 数据类型 数字 字符串 布尔类型 数组 Math 语句 条件语句(if.switch) 循环语句(for.fo ...
- Python基础(二)
本章内容: Python 运算符(算术运算.比较运算.赋值运算.逻辑运算.成员运算) 基本数据类型(数字.布尔值.字符串.列表.元组.字典.set集合) for 循环 enumrate range和x ...
- C#-#define条件编译
本文导读: C#的预处理器指令从来不会转化为可执行代码的命令,但是会影响编译过程的各个方面,常用的预处理器指令有#define.#undef.#if,#elif,#else和#endif等等,下面介绍 ...
- JQuery的核心的一些方法[扒来的]
JQuery的核心的一些方法 each(callback) '就像循环 $("Element").length; ‘元素的个数,是个属性 $("Element" ...
- WPF 自定义ContextMenu且为左键点击显示
<Button Click="Button_Click_3" Style="{StaticResource NormalButtonStyle}"> ...
- 【无私分享:ASP.NET CORE 项目实战(第六章)】读取配置文件(一) appsettings.json
目录索引 [无私分享:ASP.NET CORE 项目实战]目录索引 简介 在我们之前的Asp.net mvc 开发中,一提到配置文件,我们不由的想到 web.config 和 app.config,在 ...
- java环境搭建和写出一个Helloworld
一.安装环境和配置环境变量(必要环节) 安装java并配置环境变量 :在"系统变量"中设置3项属性,JAVA_HOME,PATH,CLASSPATH(大小写无所谓),若已存在则点击 ...
- git教程
http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000
- 磨刀不误砍柴工——VS生成事件
如果说磨刀不误砍柴工,同样用好Visual Studio,会大大增加咱.NET程序猿效率.本文说的就是Visual Studio中的生成事件,在解决方案下右击某个项目然后选择 “属性” 打开窗口后即可 ...
- 长按TextField或TextView显示中文的粘贴复制
首先要确保手机当前系统为中文,只需要在 plist 文件中添加 Localized resources can be mixed = YES 就行了