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 ...
随机推荐
- CF92B-Binary Number-(思维)
https://vjudge.net/problem/CodeForces-92B 题意:给一个长度为106的二进制数,有两种操作,第一种是除以2,第二种是末尾+1,以二进制运算,问这个二进制数最少几 ...
- vue.js操作元素属性
vue动态操作div的class 看代码: <!doctype html> <html lang="en"> <head> <meta c ...
- 【2019.7.20 NOIP模拟赛 T2】B(B)(数位DP)
数位\(DP\) 首先考虑二进制数\(G(i)\)的一些性质: \(G(i)\)不可能有连续两位第\(x\)位和第\(x+1\)位都是\(1\).因为这样就可以进位到第\(x+2\)位.其余情况下,这 ...
- ActiveMQ基础使用
概述 ActiveMQ是由Apache出品的,一款最流行的,能力强劲的开源消息总线.ActiveMQ是一个完全支持JMS1.1和J2EE 1.4规范的 JMS Provider实现,它非常快速,支持多 ...
- 数据迁移最快方式,多线程并行执行 Sql插入
前言: 由于系统升级,新开发的系统对数据验证,及数据关联做了很多优化,现需要将原历史版本的数据迁移到新系统中:原数据库大约有 1千多万数据,大约 50个表. 历史数据库命名为:A. 新系统库暂命名为 ...
- minggw 安装
windows上如果没有安装 visual studio, 也就是俗称的vs, 在安装一些带有c或者c++代码的Python模块的时候, 会报错Unable to find vcvarsall.bat ...
- python验证码处理(1)
目录 一.普通图形验证码 这篇博客及之后的系列,我会向大家介绍各种验证码的识别.包括普通图形验证码,极验滑动验证码,点触验证码,微博宫格验证码. 一.普通图形验证码 之前的博客已向大家介绍了简 ...
- shiro加密算法
第一节的时候我介绍过,shiro有很多加密算法,如md5和sha,而且还支持加盐,使得密码的解析变得更有难度,更好的保障了数据的安全性. 这里我们要介绍的是md5算法,因为比较常用.首先我们来看看md ...
- PHP json中文
json_encode 和 json_decode 只支持utf-8编码的字符串,GBK的字符串要用json就得转换成utf-8字符串 看效果 <?php header("Conten ...
- 个人项目:WC
一.GitHub项目地址:https://github.com/lseap/myWC 二.PSP表格: PSP2.1 Personal Software Process Stages 预估耗时(分钟) ...