要求

  • 给定一个二维平面的字母和一个单词,从一个字母出发,横向或纵向连接二维平面上的其他字母
  • 同一位置的字母只能使用一次

示例

  • 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的更多相关文章

  1. 刷题79. Word Search

    一.题目说明 题目79. Word Search,给定一个由字符组成的矩阵,从矩阵中查找一个字符串是否存在.可以连续横.纵找.不能重复使用,难度是Medium. 二.我的解答 惭愧,我写了很久总是有问 ...

  2. 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 ...

  3. [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 ...

  4. leetcode 79. Word Search 、212. Word Search II

    https://www.cnblogs.com/grandyang/p/4332313.html 在一个矩阵中能不能找到string的一条路径 这个题使用的是dfs.但这个题与number of is ...

  5. 【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 ...

  6. [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 ...

  7. 79. Word Search在字母矩阵中查找单词

    [抄题]: Given a 2D board and a word, find if the word exists in the grid. The word can be constructed ...

  8. Leetcode#79 Word Search

    原题地址 依次枚举起始点,DFS+回溯 代码: bool dfs(vector<vector<char> > &board, int r, int c, string ...

  9. 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 ...

随机推荐

  1. nsqlookupd:高性能消息中间件 NSQ 解析

    摘要:本篇将会结合源码介绍 nsqlookupd 的实现细节. 本篇将会结合源码介绍 nsqlookupd 的实现细节.nsqlookupd 主要流程与nsqd 执行逻辑相似,区别在于具体运行的任务不 ...

  2. C++并发与多线程学习笔记--unique_lock详解

    unique_lock 取代lock_quard unique_lock 的第二个参数 std::adopt_lock std::try_to_lock std::defer_lock unique_ ...

  3. [素数判断]P1125 笨小猴

    笨小猴 题目描述 笨小猴的词汇量很小,所以每次做英语选择题的时候都很头疼.但是他找到了一种方法,经试验证明,用这种方法去选择选项的时候选对的几率非常大! 这种方法的具体描述如下:假设maxn是单词中出 ...

  4. Recoil 中多级数据联动及数据重置的合理做法

    前情回顾 书接上回,前面引出了在数据存在级联的情况下,各下拉框之间的默认值及值变化的处理.简单回顾一下: 场景是: 地域下拉决定可选的可用区 默认选中第一个地域,通过设置 atom 的 default ...

  5. Spring Cloud Gateway 全局通用异常处理

    为什么需要全局异常处理 在传统 Spring Boot 应用中, 我们 @ControllerAdvice 来处理全局的异常,进行统一包装返回 // 摘至 spring cloud alibaba c ...

  6. Manjaro 安装教程

    1 概述 本文讲述了如何在单硬盘下对Manjaro进行安装. 2 写U盘 首先第一步是下载镜像,官网下载地址戳这里,如果下载速度慢可以选择国内镜像,比如戳这里. 笔者选择的是XFCE桌面: 下载好后将 ...

  7. Android通过Web与后台数据库交互

    2021.1.27 更新 已更新新版本博客,更新内容与原文章相比有点多,因此新开了一篇博客,请戳这里. 1 背景 开发一个App与后台数据库交互,基于MySQL+原生JDBC+Tomcat,没有使用D ...

  8. 玉帝传美猴王上天,大闹天宫之Java设计模式:命令模式

    目录 示例 改进代码 命令模式 定义 意图 主要解决问题 何时使用 优缺点 玉帝传美猴王上天 命令模式和策略模式的区别 示例 系统需要设计一个命令行界面,用户可输入命令来执行某项功能,系统的功能会不断 ...

  9. jasypt在springboot项目中遇到异常:Error creating bean with name 'enableEncryptablePropertySourcesPostProcessor' defined in class path resource

    背景 在使用jasypt对spring boot的配置文件中的敏感信息进行加密处理时,使用stater直接启动时,遇到了一个异常 <dependency> <groupId>c ...

  10. tidb初体验

    安装 docker-compose: https://github.com/pingcap/tidb-docker-compose pd tikv tidb各单个节点,单个副本,限制内存,cpu等,防 ...