[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 ...
随机推荐
- labview--http协议数据交互
最近接了一个项目,需求是要将采集到的数据,以以下要求上报,并且提供接口供上层系统下发指令. 采用restful的http协议进行交互: 输入输出参数皆为json体. 响应包含三部分: Code:业务码 ...
- 【WXS数据类型】Boolean
属性: 名称 值类型 说明 [Boolean].constructor [String] 返回值为“Boolean”,表示类型的结构字符串 方法: 原型:[Boolean].toString() 说明 ...
- Python全栈 Web(概述、HTML基础语法)
原文地址: https://yq.aliyun.com/articles/631222 ........................................................ ...
- Python全栈 MongoDB 数据库(Mongo、 正则基础、一篇通)
终端命令: 在线安装: sudo apt-get install mongodb 默认安装路径 : /var/lib/mong ...
- spring java config 初探
Java Config 注解 spring java config作为同xml配置形式的另一种表达形式,使用的场景越来越多,在新版本的spring boot中 大量使用,今天我们来看下用到的主要注解有 ...
- vivado使用感想
寒假学了一学期vivado也没有学出什么名堂:为了调试龙芯的五级流水CPU,今天肝了一下午结果还把vivado给摸清楚了,果然是以目标为导向最能出成绩. vivado开发硬件的流程 写代码 模拟仿真s ...
- 6.hdfs的存储过程
1.hdfs 怎么存储 切割存储 2. 为何每块是128m 与io读写速度有关,一般人的接受速度1s中,而磁盘的读写速度为100m/s,在读取文件时候需要硬盘寻找地址,一般读懂速度和寻找之间的比例是1 ...
- angularJS遇到的坑
最近在用angularjs做一些东西,由于学艺不精,对angularjs了解不够,导致经常会不小心掉进一些自己挖的坑里(⊙_⊙),在这里记下来,谨防又踩. 1.angularjs ng-show no ...
- Block的声明与定义语法
Block的声明 Block的声明与函数指针的声明类似 返回值类型(^变量名)(参数列表) Block的定义 ^返回值类型(参数列表) { 表达式 } 其中: 1 如果返回值类型是void,可以省略 ...
- PSP1123
PSP时间图: 类型 任务 开始时间 结束时间 净时间 中断时间 日期 开会 开会 16:17 16:50 33 0 20171027 开会 开会 17:00 17:22 22 0 20171028 ...