[刷题] 79 Word Search
要求
- 给定一个二维平面的字母和一个单词,从一个字母出发,横向或纵向连接二维平面上的其他字母
- 同一位置的字母只能使用一次
示例
- board = [ ['A','B','C','E'], ['S','F','C','S'], ['A','D','E','E'] ]
- 给定 word = "ABCCED",返回 true
- 给定 word = "SEE",返回 true
- 给定 word = "ABCB",返回 false
思路
实现
- board:要查找的区域
- word:要查找的字符串
- index:要查找字符串中的第几个字符
- startx,starty:查找起始位置
- inArea():判断是否在查找区域内
- visited:记录是否访问过
- 24-26:递归逻辑,先写好这段,再扩充其他部分
- 15-16:终止条件
1 class Solution {
2
3 private:
4 int d[4][2] = {{-1,0},{0,1},{1,0},{0,-1}};
5 int m,n;
6 vector<vector<bool>> visited;
7
8 bool inArea( int x , int y ){
9 return x >= 0 && x < m && y >= 0 && y< n;
10 }
11 // 从board[startx][starty]开始,寻找word[index...word.size()]
12 bool searchWord( const vector<vector<char>> &board, const string& word, int index,
13 int startx, int starty){
14
15 if( index == word.size() - 1 )
16 return board[startx][starty] == word[index];
17
18 if(board[startx][starty] == word[index] ){
19 visited[startx][starty] = true;
20 // 从startx,starty出发,向四个方向寻找
21 for( int i = 0 ; i < 4 ; i ++ ){
22 int newx = startx + d[i][0];
23 int newy = starty + d[i][1];
24 if( inArea(newx,newy) && !visited[newx][newy] &&
25 searchWord( board, word, index+1, newx, newy ))
26 return true;
27 }
28 visited[startx][starty] = false;
29 }
30 return false;
31 }
32 public:
33 bool exist(vector<vector<char>>& board, string word) {
34
35 m = board.size();
36 assert( m > 0);
37 n = board[0].size();
38
39 visited = vector<vector<bool>>(m, vector<bool>(n, false));
40
41 for( int i = 0 ; i < board.size() ; i ++)
42 for( int j = 0 ; j < board[i].size() ; j ++ )
43 if(searchWord( board, word, 0, i, j ) )
44 return true;
45
46 return false;
47 }
48 };
[刷题] 79 Word Search的更多相关文章
- 刷题79. Word Search
一.题目说明 题目79. Word Search,给定一个由字符组成的矩阵,从矩阵中查找一个字符串是否存在.可以连续横.纵找.不能重复使用,难度是Medium. 二.我的解答 惭愧,我写了很久总是有问 ...
- 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
Word Search Given a 2D board and a word, find if the word exists in the grid. The word can be constr ...
- [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 ...
- 79. Word Search在字母矩阵中查找单词
[抄题]: Given a 2D board and a word, find if the word exists in the grid. The word can be constructed ...
- Leetcode#79 Word Search
原题地址 依次枚举起始点,DFS+回溯 代码: bool dfs(vector<vector<char> > &board, int r, int c, string ...
- LeetCode OJ 79. Word Search
题目 Given a 2D board and a word, find if the word exists in the grid. The word can be constructed fro ...
随机推荐
- Starting Tomcat v9.0 Server at localhost' has encountered a problem
•问题描述 在通过 Eclipse 打开 Tomcat 时报错: •解决方案 找到 Tomcat 的安装位置,打开 tomcat\bin 目录,找到 shutdown.bat,手动关闭 tomcat: ...
- 关于误删除elasticSearch 索引,怎么能快速找回?
背景 之前公司小王在工作中清理elasticSearch 索引,不小心使用脚本清空了最近使用的重要索引,导致开发无法准确的进行定位生产问题,造成了很大困扰. 当时我们的生产环境中是这样配置日志系统的: ...
- H5 端 rem 适配方案与 viewport 适配
H5 端 rem 适配方案与 viewport 适配 rem rem 是 CSS3 新增的一个相对单位(root em,根 em) 只根据当前页面 HTML 页面的 font-size 设置,如果根目 ...
- ret2dl32
ret2dl32 首先检查一下保护: IDA分析一下 程序很简单就是,往bss段上的buf读入0x400个数据,然后拷贝到栈上.read_got还被置为0,这一看就是要逼着你使用ret2dlresol ...
- Unity2D项目-平台、解谜、战斗! 1.5 Player框架、技能管理组件
各位看官老爷们,这里是RuaiRuai工作室,一个做单机游戏的兴趣作坊. 前文提到,凡是有"攻击"语义的对象,在游戏中,我们给予其一个"CanFight"组件予 ...
- 在IntellJ中查看JavaDoc
1. [perference--Editor--General--Code Completion] 勾上Show the documentation popup in ** ms 2. 快速显示Ja ...
- Salesforce学习之路(九)Org的命名空间
1. 命名空间的适用场景 每个组件都是命名空间的一部分,如果Org中设置了命名空间前缀,那么需使用该命名空间访问组件.否则,使用默认命名空间访问组件,系统默认的命名空间为"c". ...
- JDBC_15_悲观锁和乐观锁
悲观锁和乐观锁 并发控制 当程序中可能出现并发操作的情况时,就需要保证在并发操作的情况下数据的准确性,以此确保当前用户和其他用户一起操作时,所得到的结果和某个用户单独操作时的结果是一样的.这种手段就叫 ...
- Day01_05_Java第一个程序 HelloWorld - java类规则
第一个程序Hello World *基础语法规则: 1. 第一个Java程序 HelloWorld! public class HelloWorld{ public static void main( ...
- PAT归纳总结——关于图的一些总结
在刷题的过程中经常会碰到一些关于图论的问题,下面我将根据自己刷题的经验来对这些问题做一个总结. 图的表示方法 解决图论中的问题首先要解决的问题就是图的表示方法这一问题,图的表示方法主要有两种,一种使用 ...