[刷题] 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 ...
随机推荐
- SpringCloud(六)分布式事务
在分布式系统中,分布式事务基本上是绕不开的, 分布式事务是指事务的参与者.支持事务的服务器.资源服务器以及事务管理器分别位于不同的分布式系统的不同节点之上 .其实就可以简单理解成在分布式系统中实现事务 ...
- 2021精选 Java面试题附答案(一)
1.什么是Java Java是一门面向对象的高级编程语言,不仅吸收了C++语言的各种优点,比如继承了C++语言面向对象的技术核心.还摒弃了C++里难以理解的多继承.指针等概念,,同时也增加了垃圾回收机 ...
- 源码级深挖AQS队列同步器
我们知道,在java中提供了两类锁的实现,一种是在jvm层级上实现的synchrinized隐式锁,另一类是jdk在代码层级实现的,juc包下的Lock显示锁,而提到Lock就不得不提一下它的核心队列 ...
- OO第一单元作业——魔幻求导
简介 本单元作业分为三次 第一次作业:需要完成的任务为简单多项式导函数的求解. 第二次作业:需要完成的任务为包含简单幂函数和简单正余弦函数的导函数的求解. 第三次作业:需要完成的任务为包含简单幂函数和 ...
- 二、python学习-函数
类型判断 1.type()直接获取类型 2.isinstance 用法一:isinstance(值,类型) 返回真或假 用法二:isinstance(值,(类型1,类型2 ...)) 有一个类型满足 ...
- Unlink学习总结
Unlink 本文参考了CTF-wiki 和glibc 源码 原理: 我们在利用 unlink 所造成的漏洞时,其实就是借助 unlink 操作来达成修改指针的效果. 我们先来简单回顾一下 unlin ...
- rpm 和 yum 软件管理
软件安装总结: 安装软件方式有如下几种: 方式1:编译安装 将源码程序按照需求进行先编译,后安装 缺点: 安装过程复杂,而且很慢 优点: 安装过程可控,真正的按需求进行安装(安装位置.安装的模块都可以 ...
- 22. VUE 插槽-详解
插槽 一直对插槽不理解,今天学习,并整理一下,希望日后可以灵活运用. (一)插槽内容 先简单来个例子,看一下插槽的租作用. 1.1 不使用插槽 父组件中: <div id="app&q ...
- Summary: DOM modification techniques
Modifying an existing element We covered various ways that you can modify aspects of an existing ele ...
- 《机器学习Python实现_10_06_集成学习_boosting_gbdt分类实现》
一.利用回归树实现分类 分类也可以用回归树来做,简单说来就是训练与类别数相同的几组回归树,每一组代表一个类别,然后对所有组的输出进行softmax操作将其转换为概率分布,然后再通过交叉熵或者KL一类的 ...