首页 > 其他分享 >79. Word Search

79. Word Search

时间:2023-03-07 13:04:36浏览次数:43  
标签:index Search used word search board Word 79 size

##题目
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.

For example,
Given board =

[
[‘A’,‘B’,‘C’,‘E’],
[‘S’,‘F’,‘C’,‘S’],
[‘A’,‘D’,‘E’,‘E’]
]

word = “ABCCED”, -> returns true,
word = “SEE”, -> returns true,
word = “ABCB”, -> returns false.
##思路
本题是典型的深度优先遍历,外层循环遍历整个二维数组,内层循环对每个节点上下左右做遍历
外层循环:

//外层循环
        for(int i=0;i<board.size();i++)
        {
            for(int j=0;j<board[0].size();j++)
            {
                if(search(board,word,0,i,j,used))
                    return true;
            }
        }

内层循环:

//DFS
        bool res = search(board,word,index+1,i-1,j,used)   
                || search(board,word,index+1,i+1,j,used)  
                || search(board,word,index+1,i,j-1,used)   
                || search(board,word,index+1,i,j+1,used);  

##代码

class Solution {
public:
    bool exist(vector<vector<char>>& board, string word) {
        if(word.empty()||word.size()==0)
            return false;
        if(board.empty()||board.size()==0||board[0].size()==0)
            return false;
        vector<vector<bool>> used(board.size(),vector<bool>(board[0].size()));
        for(int i=0;i<board.size();i++)
        {
            for(int j=0;j<board[0].size();j++)
            {
                if(search(board,word,0,i,j,used))
                    return true;
            }
        }
        return false; 
    }
private:
    bool search(vector<vector<char>> board, string word, int index, int i, int j, vector<vector<bool>> used)
    {
        if(index==word.size())
            return true;
        if(i<0||j<0||i>=board.size()|| j>=board[0].size() || used[i][j] || board[i][j]!=word[index])
            return false; 
        used[i][j] = true;
        bool res = search(board,word,index+1,i-1,j,used)   
                || search(board,word,index+1,i+1,j,used)  
                || search(board,word,index+1,i,j-1,used)   
                || search(board,word,index+1,i,j+1,used);  
        used[i][j] = false;  
        return res; 
    }
};

标签:index,Search,used,word,search,board,Word,79,size
From: https://blog.51cto.com/u_15996214/6105917

相关文章

  • Word2Vec总结
    最近一段时间,我写了好几篇关于Word2vec的文章,从理论部分到具体实践,现总结如下:理论部分轻松理解skip-gram模型轻松理解CBOW模型上述两篇博文从理论角度,讲述了Word2Vec......
  • Word2vec之情感语义分析实战(part3)--利用分布式词向量完成监督学习任务
    引言这篇博客将基于前面一篇博客Part2做进一步的探索与实战。demo代码与数据:传送门单词的数值化表示前面我们训练了单词的语义理解模型。如果我们深入研究就会发......
  • ckeditor粘贴word文档图片的功能
    ​ 这种方法是servlet,编写好在web.xml里配置servlet-class和servlet-mapping即可使用后台(服务端)java服务代码:(上传至ROOT/lqxcPics文件夹下)<%@ page language="java"......
  • 什么是Whole Word Masking
    WholeWordMasking翻译成全词Mask,是一种预训练阶段的训练样本生成策略。最原始的分词方式是基于WordPiece子词,它会把完整的一个词切分成若干个子词,在生成训练样本时,这些......
  • ElasticSearch 实现分词全文检索 - Java SpringBoot ES 索引操作
    目录ElasticSearch实现分词全文检索-概述ElasticSearch实现分词全文检索-ES、Kibana、IK安装ElasticSearch实现分词全文检索-Restful基本操作ElasticSearch......
  • Laravel9 Excel导入 和 Word导入
    Excel导入类:<?phpnamespaceApp\Utils;useIlluminate\Http\UploadedFile;useIlluminate\Support\Facades\Storage;useMaatwebsite\Excel\Facades\Excel;class......
  • ElasticSearch 实现分词全文检索 - Restful基本操作
    Restful语法GET请求:http://ip:port/index:查询索引信息http://ip;port/index/type/doc_id:查询指定的文档信息POST请求:http://ip;port/index/type/_search:......
  • CF1793 Codeforces Round 852 (Div. 2) D. Moscow Gorillas
    https://codeforces.com/contest/1793/problem/D对于给定的两个长度为\(n\)的排列\(a_i,b_i\),定义\(MEX(S)\)为集合\(S\)中没有出现的最小的正整数,找出所有满足......
  • P2679 [NOIP2015 提高组] 子串
    两个仅包含小写英文字母的字符串AA和BB。现在要从字符串AA中取出kk个互不重叠的非空子串,然后把这kk个子串按照其在字符串AA中出现的顺序依次连接起来得到一个......
  • springmvc整合thymeleaf之helloword
    版本说明:代码地址:https://gitee.com/joy521125/ssm-senior.git  thymeleaf分支;基于https://gitee.com/joy521125/ssm-senior.gitmaster分支修改而来;1.加入jar包:1......