题目

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. T-SQL 类型转换

    use StudentManageDB go --定义变量并查询 declare @sumScore int select @sumScore=(CSharp+SQLServerDB) from Sc ...

  2. golang "text/template" 模板语法简明教程

    转自:https://www.cnblogs.com/Pynix/p/4154630.html [模板标签] 模板标签用"{{"和"}}"括起来   [注释] ...

  3. kafka的安装 (单机)

    https://blog.csdn.net/q282176713/article/details/81112988

  4. SQL Server 2016:内存列存储索引

    作者 Jonathan Allen,译者 谢丽 SQL Server 2016的一项新特性是可以在“内存优化表(Memory Optimized Table)”上添加“列存储索引(Columnstor ...

  5. tomcat7.0安装笔记

    1. 解压,新增系统环境变量CATALINA_HOME,值为tomcat所在目录,如E: tomcat7.0 PS:安装JAVA时没有配置系统变量JAVA_HOME,导致报错无法启动tomcat,新建 ...

  6. websocket 群聊,单聊,加密,解密

    群聊 from flask import Flask, request, render_templatefrom geventwebsocket.handler import WebSocketHan ...

  7. LRU简单实现

    用LinkedHashMap来实现 package com.yin.purchase.dao; import java.util.ArrayList; import java.util.Collect ...

  8. gentoo openrc 开机打印信息

    gentoo openrc 开机的时候,最开始 一些硬件的信息, 后面是一些内核和驱动的信息. 硬件的信息是默认保存到 /var/log/dmesg 中, 可以使用 dmesg | less 来查看硬 ...

  9. Java执行js代码

    在做项目中有时候需要用到Java调用js文件执行相应的方法 在JDK1.6添加了新的ScriptEngine类,允许用户直接执行js代码. import org.junit.Test; import ...

  10. 35.在CSS中 只用一个 DOM 元素就能画出国宝熊猫

    原文地址:https://segmentfault.com/a/1190000015052653 感想: 真神奇! HTML code: <div class="panda" ...