一天一道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;//回溯的思想,此趟搜索没有成功,就应该把此次的标记都释放掉!
        }
    }
};

最新文章

  1. docker-registry 搭建私有仓库服务器
  2. 使用F#来实现哈夫曼编码吧
  3. 纯JavaScrip图表插件——Highcharts
  4. HDU 2196 Computer 树形DP 经典题
  5. BZOJ 1047 理想的正方形(单调队列)
  6. hdoj1847(博弈论)
  7. Visual Studio/vs2013 正忙
  8. CSS中可以通过哪些属性定义,使得一个DOM元素不显示在浏览器可视范围内?
  9. Linux crontab任务调度
  10. 【学习总结】Git学习-参考廖雪峰老师教程四-时光机穿梭
  11. Spring Boot + Spring Cloud 实现权限管理系统(解决跨域问题)
  12. 黄金分割点(第五周 c语言版)
  13. JSON 数组的创建方式
  14. Journal entry of the thirteenth chapter to chapter seventeenth(第十三章和十七章阅读与疑问)
  15. springboot中@webfilter注解的filter时注入bean都是null
  16. Pg168-1
  17. 用户从地址栏输入url,按下enter键后,直到页面加载完成的这个过程都发生了什么?
  18. 【LibreOJ】#6257. 「CodePlus 2017 12 月赛」可做题2
  19. Javase、Javaee、Javame的区别
  20. StringJoiner

热门文章

  1. Angular5 路由传参的3种方法
  2. Python3 File 方法
  3. SQL_CALC_FOUND_ROWS equivalent in PostgreSQL
  4. MySQL之sql文件的导入导出
  5. ubuntu16.04下安装opencv
  6. Twitter 架构优化之路--Twitter是如何做到每秒处理3000张图片的
  7. iOS-改变UITextField的Placeholder颜色的三种方式
  8. 深入解读XML解析
  9. [lua]luasocket.c:20:17: fatal error: lua.h: No such file or directory
  10. x264源代码简单分析:熵编码(Entropy Encoding)部分