题目描述

给出一个二维字符数组和一个单词,判断单词是否在数组中出现,
单词由相邻单元格的字母连接而成,相邻单元指的是上下左右相邻。同一单元格的字母不能多次使用。
例如:
给出的字符数组=
[↵  ["ABCE"],↵  ["SFCS"],↵  ["ADEE"]↵]

单词 ="ABCCED", -> 返回 true,
单词 ="SEE", ->返回 true,
单词 ="ABCB", -> 返回 false.


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 =

[↵  ["ABCE"],↵  ["SFCS"],↵  ["ADEE"]↵]↵

word ="ABCCED", -> returnstrue,
word ="SEE", -> returnstrue,

class Solution {
public:
    bool isOut(int r,int c,int rows,int cols){
        return c<0 || c>=cols || r<0 || r>=rows;
    }
    bool DFS(vector< vector< char >>&board,int r, int c,string &word,int start){
        if (start>=word.size())
            return true;
        if (isOut(r,c,board.size(),board[0].size() )|| word[start]!=board[r][c])
            return false;
        int dx[]={0,0,1,-1},dy[]={1,-1,0,0};
        char tmp=board[r][c];
        board [r][c]='.';
        for (int i=0;i<4;++i){
            if (DFS(board,r+dx[i],c+dy[i],word,start+1))
                return true;
        }
        board[r][c]=tmp;
        return false;
    }
    bool exist(vector<vector<char> > &board, string word) {
        int rows=board.size(),cols=board[0].size();
        for (int r=0;r<rows;++r)
            for (int c=0;c<cols;++c){
                if (board[r][c]==word[0])
                    if (DFS(board,r,c,word,0))
                        return true;
            }
            return false;
        
    }
};

leetcode70word-search的更多相关文章

  1. [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法

    二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...

  2. Leetcode 笔记 99 - Recover Binary Search Tree

    题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...

  3. Leetcode 笔记 98 - Validate Binary Search Tree

    题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...

  4. 基于WebGL 的3D呈现A* Search Algorithm

    http://www.hightopo.com/demo/astar/astar.html 最近搞个游戏遇到最短路径的常规游戏问题,一时起兴基于HT for Web写了个A*算法的WebGL 3D呈现 ...

  5. Leetcode: Convert sorted list to binary search tree (No. 109)

    Sept. 22, 2015 学一道算法题, 经常回顾一下. 第二次重温, 决定增加一些图片, 帮助自己记忆. 在网上找他人的资料, 不如自己动手. 把从底向上树的算法搞通俗一些. 先做一个例子: 9 ...

  6. [LeetCode] Closest Binary Search Tree Value II 最近的二分搜索树的值之二

    Given a non-empty binary search tree and a target value, find k values in the BST that are closest t ...

  7. [LeetCode] Closest Binary Search Tree Value 最近的二分搜索树的值

    Given a non-empty binary search tree and a target value, find the value in the BST that is closest t ...

  8. [LeetCode] Verify Preorder Sequence in Binary Search Tree 验证二叉搜索树的先序序列

    Given an array of numbers, verify whether it is the correct preorder traversal sequence of a binary ...

  9. [LeetCode] Search a 2D Matrix II 搜索一个二维矩阵之二

    Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...

  10. [LeetCode] Lowest Common Ancestor of a Binary Search Tree 二叉搜索树的最小共同父节点

    Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BS ...

随机推荐

  1. Jmeter之『Xpath提取器』

    1.使用Xpath提取时,有时候需要过滤标题,使用以下语句 //td[@class="r-count" and not(text()="回应")]

  2. 「Redis」字符串

    原文链接:https://www.changxuan.top/?p=1109 简介 Redis 中自定义的字符串结构. 字符串是 Redis 中最常用的一种数据类型,在 Redis 中专门封装了一个字 ...

  3. centos7 samba安装教程

    samba的用途:有的时候,我们需要在centos7 的文件能共享给其他机器. rpm -qa|grep samba yum install -y samba setenforce 0 sed -i ...

  4. nginx的脚本引擎(二)rewrite

    其实rewrite指令和上一篇说的if/set/return/break之类的没多大差别,但是rewrite用起来相对复杂,我就把他单独放到了这里.想要弄懂nginx的脚本引擎需要先明白处理reque ...

  5. 迅雷bt种子的制作

    BT是目前最热门的下载方式之一,它的全称为"BitTorrent"简称"BT",中文全称"比特流",但很多朋友将它戏称为"变态下载 ...

  6. 游戏2048的核心算法c#版本的实现

    接触游戏有一段时间了,也写了一些东西,效果还不错,今天没事,我就把2048 c# 版本的实现贴出来,代码已经测试过,可以正常.完美运行.当然了,在网上有很多有关2048的实现方法,但是没有提出到类里面 ...

  7. linux(centos8):jmeter5.3并发测试实例(参数在范围内随机取值)

    一,测试的url地址说明: 1,这是一个秒杀功能的url: http://127.0.0.1:8080/second/skusecond?actid=2020&skuid=cpugreen&a ...

  8. linux(centos8):lnmp环境编译安装zabbix5.0

    一,zabbix的用途: zabbix是一个基于WEB界面的提供分布式系统监视以及网络监视功能的企业级的开源解决方案 zabbix能监视各种网络参数,保证服务器系统的安全运营: 并提供灵活的通知机制以 ...

  9. 第三十五章 Linux常规练习题(二)参考答案

    一.练习题一 1.删除用户基本组shanghai03.发现无法正常删除,怎样才能将其删除掉,不能删除用户. groupdel shanghai03 移除其所属组的用户 2.打开多个xshell窗口连接 ...

  10. Linux入门到放弃之四《磁盘管理》

    一,磁盘管理 1.添加一个新磁盘/dev/sdb,用fdisk工具给磁盘分区,要求:一个主分区,一个扩展分区,两个逻辑分区: (1)去虚拟机设置添加一块硬盘,大小自定义 (2)重启系统 命令:rebo ...