题目

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.

解答

其实就是个DFS,太水了以至于一遍就AC了。。。

下面是AC的代码:

class Solution {
public:
    int **flag;
    bool search(vector<vector<char>>& board, string word, int row, int col){
        if(row < 0 || row >= board.size() || col < 0 || col >= board[0].size()){
            return false;
        }
        if(word[0] == board[row][col] && flag[row][col] == 0){
            if(word.size() == 1){
                return true;
            }
            else{
                int length = word.size();
                flag[row][col] = 1;
                int ret = search(board, word.substr(1, length - 1), row - 1, col)
                    || search(board, word.substr(1, length - 1), row, col + 1)
                    || search(board, word.substr(1, length - 1), row + 1, col)
                    || search(board, word.substr(1, length - 1), row, col - 1);
                if(ret == true){
                    return true;
                }
                else{
                    flag[row][col] = 0;
                    return false;
                }
            }
        }
        else{
            return false;
        }
    }
    bool exist(vector<vector<char>>& board, string word) {
        int row = board.size();
        int col = board[0].size();
        flag = new int*[row];
        for(int i = 0; i < row; i++){
            flag[i] = new int[col];
            for(int j = 0; j < col; j++){
                flag[i][j] = 0;
            }
        }
        for(int i = 0; i < row; i++){
            for(int j = 0; j < col; j++){
                if(search(board, word, i, j) == true){
                    return true;
                }
            }
        }
        return false;
    }
};

117

LeetCode OJ 79. Word Search的更多相关文章

  1. 【LeetCode】79. Word Search

    Word Search Given a 2D board and a word, find if the word exists in the grid. The word can be constr ...

  2. 【一天一道LeetCode】#79. Word Search

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

  3. LeetCode OJ:Word Search(单词查找)

    Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from l ...

  4. 【LeetCode】79. Word Search 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 回溯法 日期 题目地址:https://leetco ...

  5. 刷题79. Word Search

    一.题目说明 题目79. Word Search,给定一个由字符组成的矩阵,从矩阵中查找一个字符串是否存在.可以连续横.纵找.不能重复使用,难度是Medium. 二.我的解答 惭愧,我写了很久总是有问 ...

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

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

  8. leetcode 79. Word Search 、212. Word Search II

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

  9. LeetCode解题报告—— Word Search & Subsets II & Decode Ways

    1. Word Search Given a 2D board and a word, find if the word exists in the grid. The word can be con ...

随机推荐

  1. [ERR] Node is not empty. Either the node already knows other nodes (check with C

    [root@node00 src]# ./redis-trib.rb add-node --slave --master-id4f6424e47a2275d2b7696bfbf8588e8c4c3a5 ...

  2. 第7章 网络层协议(4)_IGMP协议

    4. IGMP协议(Internet Group Management Protocol) 4.1 什么是组播(多播) (1)单播同一个视频要发送90个副本,但支持收看者“快进”和“倒退”. (2)组 ...

  3. browserify babel gulp 没有编译import的文件

    1.遇到坑的gulp配置: var gulp = require('gulp'), watch = require('gulp-watch'), babel = require('gulp-babel ...

  4. tomcat8+memcached session共享

    一.环境准备 时间同步(同步后确认各服务器时间是否一致,不一致需要修改一下时区) 关闭防火墙 软件包和jar包链接:https://pan.baidu.com/s/1sl9Nob7 二.安装配置ngi ...

  5. Linux交换空间(swap space)

    每次安装Linux的时候,都会要求配置交换分区,那么这个分区是干嘛的呢?不设置这个分区有什么后果?如果一定要设置,设置多大比较合适?本篇将试图回答这些问题并尽量覆盖所有swap相关的知识. 下面的所有 ...

  6. Java捕获异常的问题

    ---恢复内容开始--- 在Java编译过程中,有时候会出现输入未按照规定输入的情况,此时需要警告用户输入错误,这就会是程序运行过程中出现异常.异常就是可预测但是又没办法消除的一种错误.所以在编写过程 ...

  7. VMware虚拟机上配置nginx后,本机无法访问问题

    nginx装在CentOS上,用本机访问虚拟机的时候却出现了不能访问的问题,查了资料以后,原来是防火墙的问题.具体情况如下:防火墙可以ping通虚拟机,虚拟机也可以ping通防火墙.接着检查了服务器端 ...

  8. flask 之request用法

    每个框架中都有处理请求的机制(request),但是每个框架的处理方式和机制是不同的 为了了解Flask的request中都有什么东西,首先我们要写一个前后端的交互 基于HTML + Flask 写一 ...

  9. StanFord ML 笔记 第三部分

    第三部分: 1.指数分布族 2.高斯分布--->>>最小二乘法 3.泊松分布--->>>线性回归 4.Softmax回归 指数分布族: 结合Ng的课程,在看这篇博文 ...

  10. fabric-network_setup.sh安装脚本分析

    在进行镜像安装前,提供了一个sample脚本的下载,可以使用该脚本进行容器的启停.这里对该脚本进行分析. fabric/release/linux-amd64/network_setup.sh 脚本提 ...