【一天一道LeetCode】#79. Word Search
一天一道LeetCode
本系列文章已全部上传至我的github,地址:ZeeCoder‘s Github
欢迎大家关注我的新浪微博,我的新浪微博
欢迎转载,转载请注明出处
(一)题目
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 =[
[‘A’,’B’,’C’,’E’],
[‘S’,’F’,’C’,’S’],
[‘A’,’D’,’E’,’E’]
]
word = “ABCCED”, -> returns true,
word = “SEE”, -> returns true,
word = “ABCB”, -> returns false.
(二)解题
本题大意:在一个字母矩阵中搜索指定的单词,要求矩阵中相邻的字母(上下左右)连接起来组成指定的单词,矩阵中的字母不允许重复使用。
解题思路:
1、采用回溯法和动态规划来解决
2、每次搜索到首字母后就向四个方向继续搜索,知道连接起来组成整个指定单词为止
3、注意字母不能重复搜索,需要用一个矩阵来标记此次搜索过的单词
下面来看具体代码:
class Solution {
public:
bool isExist = false;
bool exist(vector<vector<char>>& board, string word) {
if(word == "") return true;//单词为空的特殊情况
int row = board.size();
if(row == 0) return false;
int col = board[0].size();
vector<vector<bool>> isSearch;
for(int i = 0 ; i < row ; i++)//初始化用来标记已搜索过字母的矩阵
{
vector<bool> temp(col,false);
isSearch.push_back(temp);
}
for (int i = 0; i < row;i++)
{
for (int j = 0; j < col;j++)
{
if(board[i][j] == word[0]) {//如果找到首字母就开始从四个方向搜索
backTraceExist(board, word, 0,i, j, row ,col ,isSearch);
}
}
}
return isExist;
}
void backTraceExist(vector<vector<char>>& board, string word , int count ,int x , int y, int& row,int& col,vector<vector<bool>>& isSearch)
{
if(board[x][y] == word[count]) count++;//如果相等
else{
return;
}
if(count == word.length())//结束标志
{
isExist = true;
return;
}
if(!isExist){
isSearch[x][y] = true;//找过的字母记得标记起来
//这里需要注意越界的问题
//向左边找
if(y-1>=0&&!isSearch[x][y-1]) backTraceExist(board,word,count,x,y-1,row,col,isSearch);
//向右边找
if(y+1<col&&!isSearch[x][y+1]) backTraceExist(board,word,count,x,y+1,row,col,isSearch);
//向上找
if(x-1>=0&&!isSearch[x-1][y]) backTraceExist(board,word,count,x-1,y,row,col,isSearch);
//向下找
if(x+1<row&&!isSearch[x+1][y]) backTraceExist(board,word,count,x+1,y,row,col,isSearch);
isSearch[x][y] = false;//回溯的思想,此趟搜索没有成功,就应该把此次的标记都释放掉!
}
}
};
【一天一道LeetCode】#79. 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] 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 79. Word Search 、212. Word Search II
https://www.cnblogs.com/grandyang/p/4332313.html 在一个矩阵中能不能找到string的一条路径 这个题使用的是dfs.但这个题与number of is ...
- 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 79 Word Search(单词查找)
题目链接:https://leetcode.com/problems/word-search/#/description 给出一个二维字符表,并给出一个String类型的单词,查找该单词是否出现在该二 ...
- 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#79 Word Search
原题地址 依次枚举起始点,DFS+回溯 代码: bool dfs(vector<vector<char> > &board, int r, int c, string ...
- [LeetCode] 212. Word Search II 词语搜索 II
Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...
- Java for LeetCode 212 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】#81. Search in Rotated Sorted Array II
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Follow ...
随机推荐
- WIN2008虚拟机安装ORACLE11g记录
---恢复内容开始--- 1.ORACLE11g的安装包下载与解压 官网下载地址:(http://www.oracle.com/technetwork/database/enterprise-edit ...
- Python中and(逻辑与)计算法则
在程序设计中,and称为逻辑与运算,也称布尔运算:1.and是在布尔上下文中从左到右计算表达式的值:2.0.''.[].().{}.None.False在布尔上下文中为假:其它任何东西都为真:3.如果 ...
- [self init]
在字典转模型中遇到了这样的代码: #import "HMAppInfo.h" @implementation HMAppInfo - (instancetype)initWithD ...
- JAVA生成数字0~9字母A~Z混合编码0000、0001...0009、000A...000Z、0010......
分别是求下一个编码 和 输出所有编码 /** * 用1--9加A--Z混合编码 使用ASCII码判断 * LYL * 传一个值 求下一个编码 */ public String getABCDCode( ...
- getParameter的用法总结
getParameter得到的都是String类型的.或者是用于读取提交的表单中的值(http://a.jsp?id=123中的123),或者是某个表单提交过去的数据: getAttribute则可以 ...
- android ActionBarActivity设置全屏无标题
新建的Activity继承自ActionBarActivity,设置全屏无标题本来很简单的事,但是没想到app竟然无缘无故的挂,要么就是白屏一片,要么就是黑屏.坑了我一个多小时!!! 原因是Actio ...
- 剑指Offer——知识点储备-J2EE基础
剑指Offer--知识点储备-J2EE基础 9.2 jdk 1.8的新特性(核心是Lambda 表达式) 参考链接:http://www.bubuko.com/infodetail-690646.ht ...
- Ubuntu Intel显卡驱动安装 (Ubuntu 14.04--Ubuntu 16.10 + Intel® Graphics Update Tool)
最近使用在使用Ubuntu时,发现大部分情况下,不安装显卡驱动,使用默认驱动,都是没有问题的,但对于一些比较奇特配置的电脑,如下所示,如果使用默认驱动,会时常莫名其妙死机crash,尤其是在使用Ope ...
- Core Python Programming一书中关于深浅拷贝的错误
该书关于深浅拷贝的论述: 6.20. *Copying Python Objects and Shallow and Deep Copies "when shallow copies are ...
- SRAM/DRAM,PROM/EPROM/EEPROM,NOR/NAND FLASH区别
SRAM/DRAM,PROM/EPROM/EEPROM,NOR/NAND FLASH区别 RAM / ROM 存储器 ROM和RAM指的都是半导体存储器,R ...