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 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.
Example:
board =
[
['A','B','C','E'],
['S','F','C','S'],
['A','D','E','E']
] Given word = "ABCCED", return true.
Given word = "SEE", return true.
Given word = "ABCB", return false.
分析:
给定一个二维网格和一个单词,找出该单词是否存在于网格中。
单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中“相邻”单元格是那些水平相邻或垂直相邻的单元格。同一个单元格内的字母不允许被重复使用。
一道搜索的题目,有点类似走迷宫,只不过按照给定单词字母顺序来寻找,遍历board中每一个元素,判断与word中的第一个字母是否相同,如果相同则在当前位置上去搜索上下左右相邻的单元格的元素是否和当前字母的下一个字母相同,不在搜索范围内或者字母不同就返回false,当搜索的字母数等于word的长度时,也就表明在board找到了这个word。注意每次判断一个字母要标记当前位置以搜索过,以防止字母重复利用。我选择直接更改board元素,以便在后续的判断中不会重复判断此位置,在搜索结束后改回来就可以了。
程序:
class Solution {
public:
bool exist(vector<vector<char>>& board, string word) {
h = board.size();
w = board[].size();
for(int i = ; i < h; i++){
for(int j = ; j < w; ++j){
if(searchexist(board, word, , i, j)) return true;
}
}
return false;
}
int searchexist(vector<vector<char>>& board, string &word, int n, int x, int y){
if(x < || x > h- || y < || y > w- || word[n] != board[x][y])
return ;
if(n == word.length()-)
return ;
char temp = board[x][y];
board[x][y] = ;
int flag = searchexist(board, word, n+, x+, y)
||searchexist(board, word, n+, x-, y)
||searchexist(board, word, n+, x, y+)
||searchexist(board, word, n+, x, y-);
board[x][y] = temp;
return flag;
}
private:
int h, w;
};
LeetCode 79. Word Search单词搜索 (C++)的更多相关文章
- [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 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 ...
- 079 Word Search 单词搜索
给定一个二维面板和一个单词,找出该单词是否存在于网格中.这个词可由顺序相邻的单元格内的字母构成,其中“相邻”单元格是那些水平相邻或垂直相邻的单元格.同一个单元格内的字母不允许被重复使用.例如,给定 二 ...
- Leetcode79. Word Search单词搜索
给定一个二维网格和一个单词,找出该单词是否存在于网格中. 单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中"相邻"单元格是那些水平相邻或垂直相邻的单元格.同一个单元格内的字 ...
- Leetcode#79 Word Search
原题地址 依次枚举起始点,DFS+回溯 代码: bool dfs(vector<vector<char> > &board, int r, int c, string ...
随机推荐
- [LOJ 6433][PKUSC 2018]最大前缀和
[LOJ 6433][PKUSC 2018]最大前缀和 题意 给定一个长度为 \(n\) 的序列, 求把这个序列随机打乱后的最大前缀和的期望乘以 \(n!\) 后对 \(998244353\) 取膜后 ...
- WIFI Portal登录
开头 关于 ANDROID 5.0-7.1.2 网络图标上的感叹号及其解决办法-狐狸的小小窝 HTTP状态码之204 No Content 原理 访问generate_204地址,如果得到状态码是20 ...
- POJ 3252 (数位DP)
###POJ 3252 题目链接 ### 题目大意:给你一段区间 [Start,Finish] ,在这段区间中有多少个数的二进制表示下,0 的个数 大于等于 1 的个数. 分析: 1.很显然是数位DP ...
- Tensorflow faster rcnn系列一
注意:本文主要是学习用,发现了一个在faster rcnn训练流程写的比较详细的博客. 大部分内容来自以下博客连接:https://blog.csdn.net/weixin_37203756/arti ...
- redis命令之 ----Set(集合)
SADD SADD key member [member ...] 将一个或多个 member 元素加入到集合 key 当中,已经存在于集合的 member 元素将被忽略. 假如 key 不存在,则创 ...
- 禁用,移除 WPF window窗体系统操作SystemMenu
public static class SystemMenuManager { [DllImport("user32.dll", EntryPoint = "GetSys ...
- JDK1.8新特性——Optional类
JDK1.8新特性——Optional类 摘要:本文主要学习了JDK1.8新增加的Optional类. 部分内容来自以下博客: https://www.cnblogs.com/1ning/p/9140 ...
- Linux之Shell编程(15)
case: for: while:
- jQuery AJAX方法详谈
AJAX是与服务器交换数据并更新部分网页的技术,而无需重新加载整个页面. 下表列出了所有jQuery AJAX方法: 方法 描述 $.ajax() 执行异步AJAX请求 $.ajaxPrefilter ...
- JavaScript Location 对象用法
Location 对象 Location对象包含有关当前URL的信息.location对象是window对象的一部分,可以通过window.location属性访问. 注意:没有适用于location ...