[刷题] 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 ...
随机推荐
- python自动统计zabbix系统监控覆盖率
脚本主要功能: 1)通过zabbix api接口采集所有监控主机ip地址: 2)通过cmdb系统(蓝鲸)接口采集所有生产主机IP地址.主机名.操作系统.电源状态: 3)以上2步返回数据对比,找出未监控 ...
- HTTP2和 HTTPS来不来了解一下?
本文力求简单讲清每个知识点,希望大家看完能有所收获 一.HTTP协议的今生来世 最近在看博客的时候,发现有的面试题已经考HTTP/2了,于是我就顺着去了解一下. 到现在为止,HTTP协议已经有三个版本 ...
- elementui 表格 如何使操作中隐藏一个按钮
<el-table-column label="权限"min-width="100"> <template scope="scope ...
- 添加ASP.NET文件夹(3)
注意:这里添加的是ASP.NET文件夹,而不是普通网站下项目创建的文件夹 ASP.NET文件夹主要有7个 1.Bin文件夹包含程序所需的所有已编译程序集 2.App_Code文件夹包含页所使用的类的源 ...
- 数据库MySQL五
测试题复习 子查询案例 DML语句(很重要) 自增长列 为某一个字段设置自增长 修改语句 truncate实际上是DDL语句删除表再新建一个表 DCL事务 ACID 回滚:没发生 提交才更新数据 /* ...
- JAVAEE_Servlet_19_重定向可以解决页面刷新问题(sendRedirect)
重定向可以解决页面刷新问题(sendRedirect) 在向数据库中添加数据的时候,如果使用转发(getRequestDispatcher),数据插入成功后,转发到提示插入成功页面,在数据插入成功页面 ...
- 你已经用上 5G 网络了吗?
随着各大手机厂商陆续推出 5G 手机,智能手机全面迎来 5G 浪潮.可能有人会发问:如此推崇 5G,5G 能为我们带来什么,我们的生活又会因此而改变多大呢? 什么是 5G? 简单地说,5G 就是第五代 ...
- java面试一日一题:讲下redo log
问题:请讲下redo log的作用 分析:mysql中有很多日志,例,binlog undo log redo log,要弄清楚这些日志的作用,就要了解这些日志出现的背景及要解决的问题? 回答要点: ...
- vue route 跳转
index.js { path: '/grouporder/grouporderdetail/id/:id', name: '/grouporder/grouporderdetail/id/', co ...
- 【Springboot】Springboot自动装配原理
1.核心注解就是 EnableAutoConfiguration 该注解会激活SpringBoot的自动装配功能: 代码如下: @Target(ElementType.TYPE) @Retentio ...