一天一道LeetCode

本系列文章已全部上传至我的github,地址:ZeeCoder‘s Github

欢迎大家关注我的新浪微博,我的新浪微博

欢迎转载,转载请注明出处

(一)题目

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.

(二)解题

本题大意:在一个字母矩阵中搜索指定的单词,要求矩阵中相邻的字母(上下左右)连接起来组成指定的单词,矩阵中的字母不允许重复使用。

解题思路:

1、采用回溯法和动态规划来解决

2、每次搜索到首字母后就向四个方向继续搜索,知道连接起来组成整个指定单词为止

3、注意字母不能重复搜索,需要用一个矩阵来标记此次搜索过的单词

下面来看具体代码:

class Solution {
public:
    bool isExist = false;
    bool exist(vector<vector<char>>& board, string word) {
        if(word == "") return true;//单词为空的特殊情况
        int row = board.size();
        if(row == 0) return false;
        int col = board[0].size();
        vector<vector<bool>> isSearch;
        for(int i = 0 ; i < row ; i++)//初始化用来标记已搜索过字母的矩阵
        {
            vector<bool> temp(col,false);
            isSearch.push_back(temp);
        }
        for (int i = 0; i < row;i++)
        {
            for (int j = 0; j < col;j++)
            {
                if(board[i][j] == word[0]) {//如果找到首字母就开始从四个方向搜索
                    backTraceExist(board, word, 0,i, j, row ,col ,isSearch);
                }
            }
        }
        return isExist;
    }
    void backTraceExist(vector<vector<char>>& board, string word , int count ,int x , int y, int& row,int& col,vector<vector<bool>>& isSearch)
    {
        if(board[x][y] == word[count]) count++;//如果相等
        else{
            return;
        }
        if(count == word.length())//结束标志
        {
            isExist = true;
            return;
        }
        if(!isExist){
            isSearch[x][y] = true;//找过的字母记得标记起来
            //这里需要注意越界的问题
            //向左边找
            if(y-1>=0&&!isSearch[x][y-1]) backTraceExist(board,word,count,x,y-1,row,col,isSearch);
            //向右边找
            if(y+1<col&&!isSearch[x][y+1]) backTraceExist(board,word,count,x,y+1,row,col,isSearch);
            //向上找
            if(x-1>=0&&!isSearch[x-1][y]) backTraceExist(board,word,count,x-1,y,row,col,isSearch);
            //向下找
            if(x+1<row&&!isSearch[x+1][y]) backTraceExist(board,word,count,x+1,y,row,col,isSearch);
            isSearch[x][y] = false;//回溯的思想,此趟搜索没有成功,就应该把此次的标记都释放掉!
        }
    }
};

【一天一道LeetCode】#79. Word Search的更多相关文章

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

  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 、212. Word Search II

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

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

  5. LeetCode 79 Word Search(单词查找)

    题目链接:https://leetcode.com/problems/word-search/#/description 给出一个二维字符表,并给出一个String类型的单词,查找该单词是否出现在该二 ...

  6. LeetCode 79. Word Search单词搜索 (C++)

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

  7. Leetcode#79 Word Search

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

  8. [LeetCode] 212. Word Search II 词语搜索 II

    Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...

  9. Java for LeetCode 212 Word Search II

    Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...

  10. 【一天一道LeetCode】#81. Search in Rotated Sorted Array II

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Follow ...

随机推荐

  1. sublime安装配置

    http://www.sublimetext.com.cn/ 打华东师范大学校赛的时候,学长谈论到这个编辑器.自定义背景多行多光标同时编辑酷炫爆了.感觉这是一个万能的文本编辑器.通过配置可以写多种语言 ...

  2. 初识Spring Boot框架(二)之DIY一个Spring Boot的自动配置

    在上篇博客初识Spring Boot框架中我们初步见识了SpringBoot的方便之处,很多小伙伴可能也会好奇这个Spring Boot是怎么实现自动配置的,那么今天我就带小伙伴我们自己来实现一个简单 ...

  3. 利用git pull的勾子实现敏捷部署

    监听端 例如nginx或Python,php,rails等后端 git --git-dir=~/op/.git --work-tree=~/op pull git hooks端 位于.git/hook ...

  4. TCP的TIME_WAIT状态

    主动关闭的Socket端会进入TIME_WAIT状态,并且持续2MSL时间长度,MSL就是maximum segment lifetime(最大分节生命期),这是一个IP数据包能在互联网上生存的最长时 ...

  5. 等价于n*n的矩阵,填写0,1,要求每行每列的都有偶数个1 (没有1也是偶数个),问有多少种方法。

    #define N 4 /* * 公式: * f(n) = 2^((n - 1) ^2) */ int calWays(int n) { int mutiNum = (n - 1) * (n - 1) ...

  6. Android必知必会-长按返回健退出

    背景 平常比较常见的都是一定时间间隔内按两次返回键来退出应用,并且第一次点击会有相应的提示,网上资料比较多,这里写一下,长按返回键退出. 实现 实现的方案常用的有两个: 重写dispatchKeyEv ...

  7. EBS开发附件上传和下载功能

    上传 Oracle ERP二次开发中使用的方式有两种,一是通过标准功能,在系统管理员中定义即可,不用写代码,就可以使几乎任何Form具有附件功能,具体参考系统管理员文档:二是通过PL/SQL Gate ...

  8. springMVC源码分析--动态样式ThemeResolver(一)

    Spring MVC中通过ThemeSource接口来提供对动态更换样式的支持,并提供了ResourceBundleThemeSource这个具体实现类来提供通过properties配置文件对them ...

  9. EXCEL技能之数据去重

    本篇不属于技术类博文,只是想找个地方记录而已,既然是我的博客嘛,那就自己想写什么就写什么了. CRM中有个EXCEL数据导入功能,几千条数据导入CRM后去重,那是死的心都有的.往回想想EXCEL是否有 ...

  10. Xcode中的变量模板(variable template)的用法

    大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请多提意见,如果觉得不错请多多支持点赞.谢谢! hopy ;) 你可能经常会写一些小的代码片段,里面自然少不了一些关键的变量. ...