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 ...
随机推荐
- vs code 中配置git go
{ "window.zoomLevel": 1, "editor.fontSize": 15, //"files.autoSave": &q ...
- NAT技术详解
一.IPv4协议和NAT的由来 1.IPv4协议介绍 2011年2月3日,IANA宣布:IPv4地址空间最后5个地址块已经被分配给下属的5个地区委员会.2011年4月15日,亚太区委员会APNIC对外 ...
- 【转】UML之类图和对象图
思路呈现 什么是类图? 描述类.接口.协作及他们之间的关系的图.显示系统中类的静态结构. 有什么作用? 描述软件系统的静态结构 ①对系统的词汇建模 ②对简单协作建模 ③对逻辑数据库模式建模 什么是对象 ...
- sessionStorage 、localStorage 、 cookie 和session之间的区别
四者的异同 特性 Session Cookie localStorage sessionStorage 数据的生命期 在一定时间内保存在服务器上.当访问增多,会比较占用你服务器的性能,考虑到减 ...
- CodeForces 463D DP
Gargari got bored to play with the bishops and now, after solving the problem about them, he is tryi ...
- python Condition
import threading # 必须要使用condition的例子 # class XiaoAi(threading.Thread):# def __init__(self, lock):# s ...
- centos6 cgroup及cgred简介和简单使用
一.cgroup简介 Linux CGroup全称Linux Control Group, 是Linux内核的一个功能,用来限制,控制与分离一个进程组群的资源(如CPU.内存.磁盘输入输出等).这个项 ...
- IO流总结---- 字节流 ,字符流, 序列化 ,数据操作流,打印流 , Properties 集合
笔记内容: 什么是流 字节流 字符流 序列化 数据操作流(操作基本数据类型的流)DataInputStream 打印流 Properties 集合 什么是流: 流是个抽象的概念,是对输入输出设备的抽象 ...
- 快速上手Mac效率神器Alfred以及Alfred常用操作
前言 Alfred,想必大家就算没用过也耳闻过.Alfred是一个让你可以丢掉鼠标的神器.很多读者可能之前认为Alfred的学习成本高,或者感觉它太复杂,而望之却步.其实Alfred并非高不可攀,本文 ...
- xcodeinstruments 内存检测
http://blog.csdn.net/totogo2010/article/details/8233565