[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", -> returnstrue,
word ="SEE", -> returnstrue,
word ="ABCB", -> returnsfalse.
题意:从一个二维矩阵中找到给定单词,某个单词的下一个字符,只能从其上下左右中寻找。
思路:刚开始想到用广搜,但是,因为单词的首字符在二维数组中可能有不止一个与其对应,不合适,所以正确合适的开启方式是DFS。后来再一想,这题和surrounded regions类似,用DFS对当前位置的上下左右进行深搜,若和单词中的下一个字符相等,则从单词的下一个位置开始,重新搜索。因为题总要求,在某一次的搜索过程中,字符只能使用一次,所以对搜索过的字符要设置标识符标识已经访问过了。设置标识符有两种方法一种是:将访问过的字符设置为其他的字符,如'#',等这次搜索返回时要将其恢复到原来的字符;二是,开辟一个和元素组等大小的二维数组,标识某个字符是否被访问过,不过还是得恢复。
参考JustDoIT,代码一:
class Solution {
public:
bool exist(vector<vector<char> > &board, string word)
{
if(word.size()==) return true;
int row=board.size(),col=board[].size();
if(row==||col==) return false;
for(int i=;i<row;++i)
{
for(int j=;j<col;++j)
{
if(board[i][j]==word[]&&dfs(i,j,word,,board))
return true;
}
}
return false;
}
bool dfs(int row,int col,string &word,int index,vector<vector<char>> &board)
{
if(index==word.size()-) return true;
char temp=board[row][col];
board[row][col]='#';
if(row>&&board[row-][col]==word[index+])
if(dfs(row-,col,word,index+,board))
return true;
if(row<board.size()-&&board[row+][col]==word[index+])
if(dfs(row+,col,word,index+,board))
return true;
if(col>&&board[row][col-]==word[index+1])
if(dfs(row,col-,word,index+,board))
return true;
if(col<board[].size()-&&board[row][col+]==word[index+])
if(dfs(row,col+,word,index+,board))
return true;
board[row][col]=temp;
return false;
}
};
参考Grandyang代码二:通过使用二维标识数组来实现,但要避免使用vector<bool>,原因见这里。代码见下:
class Solution {
public:
bool exist(vector<vector<char> > &board, string word)
{
if(word.empty()) return true;
if(board.empty()||board[].empty()) return false;
//bool型的数组
vector<vector<bool>> visited(board.size(),vector<bool>(board[].size(),false));
for(int i=;i<board.size();++i)
for(int j=;j<board[i].size();++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;
if(i<||j<||i>=board.size()||j>=board[].size()||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;
}
};
针对第一方法用第二方法的形式简化,代码如下:
class Solution {
public:
bool exist(vector<vector<char> > &board, string word)
{
if(word.size()==) return true;
int row=board.size(),col=board[].size();
if(row==||col==) return false;
for(int i=;i<row;++i)
{
for(int j=;j<col;++j)
{
if(board[i][j]==word[]&&dfs(i,j,word,,board))
return true;
}
}
return false;
}
bool dfs(int row,int col,string &word,int index,vector<vector<char>> &board)
{
if(index==word.size()) return true;
char temp=board[row][col];
if(row<||row>=board.size()||col<||col>=board[].size()||board[row][col] =='#'
||board[row][col] !=word[index])
return false;
board[row][col]='#';
bool res=dfs(row-,col,word,index+,board)
||dfs(row+,col,word,index+,board)
||dfs(row,col-,word,index+,board)
||dfs(row,col+,word,index+,board);
board[row][col]=temp;
return res;
}
};
注意方法一和方法二的下标范围。
[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] 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 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 Squares 单词平方
Given a set of words (without duplicates), find all word squares you can build from them. A sequence ...
- [LeetCode] Word Frequency 单词频率
Write a bash script to calculate the frequency of each word in a text file words.txt. For simplicity ...
- [LeetCode] Word Abbreviation 单词缩写
Given an array of n distinct non-empty strings, you need to generate minimal possible abbreviations ...
随机推荐
- postman使用感言
这段时间接口测试一直使用的postman,一款谷歌接口测试插件,感受如下 优点: 1.对于中小型公司来说应该是够用的,特别是一键接口环境切换,一键设置header,作为一般的接口测试来说已经很不错了, ...
- Python字符串操作大全(非常全!!!)
1. python编程里字符串的内置方法(非常全) capitalize() 把字符串的第一个字符改为大写 casefold() 把整个字符串的所有字符改为小写 center(width) 将字符串居 ...
- Siki_Unity_1-5_见缝插针
1-5 见缝插针 任务1:资源下载 任务2:案例演示 任务3:创建工程和场景 Project Name:StickPin import素材,为两张png图 创建各个分类文件夹Scenes/ Prefa ...
- Java应用基础微专业-设计篇
第1章--抽象与接口 1.1 抽象 An abstract class can be created without abstract methods, the purpose of doing th ...
- 【第二章】Shell 变量
一.什么是变量? 变量就是一个固定的字符串(也可以是字符.数字的组合)代替更多.更复杂的内容,该内容可能是变量.路径.字符串等其他内容. 变量就是程序中保存用户数据的一块内存空间,而变量名就是这块内存 ...
- UVa 1583 - Digit Generator 解题报告 - C语言
1.题目大意 如果a加上a的各个数字之和得到b,则说a是b的生成元.给出n其中$1\le n\le 100000$,求其最小生成元,若没有解则输出0. 2.思路 使用打表的方法打出各个数字a对应的b, ...
- Java内存管理特点
Java内存管理特点 Java一个最大的优点就是取消了指针,由垃圾收集器来自动管理内存的回收.程序员不需要通过调用函数来释放内存. 1.Java的内存管理就是对象的分配和释放问题. 在 ...
- [leetcode-693-Binary Number with Alternating Bits]
Given a positive integer, check whether it has alternating bits: namely, if two adjacent bits will a ...
- H5页面 绝对定位元素被 软键盘弹出时顶起
H5页面 绝对定位元素被 软键盘弹出时顶起 在h5页面开发的过程中,我们可能会遇到下面这个问题,当页面中有输入框的时候,系统自带的软盘会把按钮挤出原来的位置.那么我们该怎么解决呢?下面列出一下的方法: ...
- Java学习个人备忘录之线程间的通信
线程间通讯多个线程在处理同一资源,但是任务却不同. class Resource { String name; String sex; } //输入 class Input implements Ru ...