一天一道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. python 的字符串驻留机制

    我们都知道python中的引用计数机制,相同对象的引用其实都是指向内存中的同一个位置,这个也叫做“python的字符串驻留机制”.其他的就不多说了,自行研究. 重点!!!!!! python的引用计数 ...

  2. JavaScript Boolean(布尔)对象

    Boolean(布尔)对象用于将非布尔值转换为布尔值(true 或者 false). Boolean(布尔)对象是三种包装对象:Number.String和Boolean中最简单的一种,它没有大量的实 ...

  3. Java第2次实验提纲(Java基本语法与类库)

    1. 使用Git克隆(clone)项目到你的Eclipse项目中 见以下参考资料中的3 从码云将项目clone到你的电脑 重要提示: 使用Git来管理你的代码以后,当你在本机Eclipse项目中开始编 ...

  4. Android Studio: You need to use a Theme.AppCompat theme (or descendant) with this activity.

    错误描述为: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with ...

  5. iOS图形手势识别框架SGGestureRecognizer

    简介 苹果官方为我们提供了简单手势的识别器,但对于图形手势,例如五角星.三角形等的识别,就需要自己实现了.通过识别这些手势,可以去执行特定的操作,或是输入公式.释放魔法等,可以为App增光添彩. 下载 ...

  6. 修改CUSTOM.PLL文件调用客户化FORM&修改标准FORM

    修改custom.pll文件里 的过程event:参考例子如下,修改好后上传至$AU_TOP/resource 运行编译frmcmp_batch CUSTOM apps/apps module_typ ...

  7. Java进阶(四十七)Socket通信

    Java进阶(四十七)Socket通信   今天讲解一个 Hello Word 级别的 Java Socket 通信的例子.具体通讯过程如下: 先启动Server端,进入一个死循环以便一直监听某端口是 ...

  8. Android Multimedia框架总结(八)Stagefright框架之AwesomePlayer及数据解析器

    转载请把头部出处链接和尾部二维码一起转载,本文出自逆流的鱼:http://blog.csdn.net/hejjunlin/article/details/52503057 前言:前面一篇分析了medi ...

  9. Apache commons email 使用过程中遇到的问题

    apache-commons-email是对mail的一个封装,所以使用起来确实是很方便.特别的,官网上的tutorial也是极其的简单.但是我也仍然是遇到了没有解决的问题. jar包的添加 mail ...

  10. x264源代码简单分析:x264命令行工具(x264.exe)

    ===================================================== H.264源代码分析文章列表: [编码 - x264] x264源代码简单分析:概述 x26 ...