一天一道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. 01_Struts2概述及环境搭建

    1.Struts2概述: Struts2是一个用来开发MVC应用程序的框架. Struts2提供了web应用程序开发过程中一些常见问题的解决方案; 对用户输入的数据进行合法性验证 统一的布局 可扩展性 ...

  2. ACM Piggy Bank

    Problem Description Before ACM can do anything, a budget must be prepared and the necessary financia ...

  3. PHP 5 Filesystem 函数

    PHP Filesystem 简介 Filesystem 函数允许您访问和操作文件系统. 安装 Filesystem 函数是 PHP 核心的组成部分.无需安装即可使用这些函数. Runtime 配置 ...

  4. JavaScript If…Else 语句

    条件语句用于基于不同的条件来执行不同的动作. 条件语句 通常在写代码时,您总是需要为不同的决定来执行不同的动作.您可以在代码中使用条件语句来完成该任务. 在 JavaScript 中,我们可使用以下条 ...

  5. Linux下的有用命令

    在之前的博客<Linux下常用命令与使用技巧>中,介绍了Linux的常用命令,在今天的博客中,给大家介绍其他的有用命令. 1.文本转换命令 在Linux下工作,我们不可避免地要和文件格式做 ...

  6. nginx 日志分析工具goaccess

    参考:https://www.goaccess.io/download 安装 $ wget http://tar.goaccess.io/goaccess-1.1.1.tar.gz $ tar -xz ...

  7. pycallgraph 追踪Python函数内部调用

    安装 安装pycallgraph 安装依赖 使用 待测脚本 追踪脚本 追踪结果 高级篇 隐藏私密函数 控制最大追踪深度 总结 GitHub上好代码真的是太多了,名副其实的一个宝藏.但是最近自己也反思了 ...

  8. 高通开发笔记---yukon worknote

    点击打开链接 daily buildhttp://android-ci-platform.cnbj.sonyericsson.net/job/daily_build_jb-mr2-yukon/DL-C ...

  9. Lua热更新(hotfix)

    Lua热更新(hotfix)(金庆的专栏)hotfixLua 5.2/5.3 hotfix. Hot update functions and keep old data.https://github ...

  10. C++ 虚函数表 多重继承

    上次研究的是单继承的情况,这次研究多重继承下的虚函数表的排列情况. 这次A,A1,A2,B这几个类的继承关系如下图: 测试代码如下: #include<iostream> using na ...