题目描述

给出一个二维字符数组和一个单词,判断单词是否在数组中出现,
单词由相邻单元格的字母连接而成,相邻单元指的是上下左右相邻。同一单元格的字母不能多次使用。
例如:
给出的字符数组=
[↵  ["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. Python装饰器实现带参数和不带参数

    1 def log(text=None): 2 3 if isinstance(text, str): 4 def decorator(func): 5 @functools.wraps(func) ...

  2. RHSA-2017:2930-重要: 内核 安全和BUG修复更新(需要重启、存在EXP、本地提权、代码执行)

    [root@localhost ~]# cat /etc/redhat-release CentOS Linux release 7.2.1511 (Core) 修复命令: 使用root账号登陆She ...

  3. npm npx cnpm yarn 的区别

    npm npm 是 Node.js 官方提供的包管理工具.用于 Node.js 包的发布.传播.依赖控制.npm 提供了命令行工具,使你可以方便地下载.安装.升级.删除包,也可以让你作为开发者发布并维 ...

  4. 1.ffmpeg、ffplay、ffprobe命令使用

    1.学前知识 1.1视频码率值 码率公式: 码率(kbps)=文件大小(KB)*8/时间(秒) 所以码率和视频文件大小成正比的,不过码率超过一定值后,人眼是看不出效果的. 接下来,我们便先来学习ffm ...

  5. 多测师讲解html _有序列表005_高级讲师肖sir

    <!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>有 ...

  6. CV学习日志:CV开发常用库及其头文件

    CV开发过程中,通常会涉及以下库:(1)语言/视觉:C.CPP.QT.OpenCV(2)通信/模拟:ROS2.Gazebo.Webots(3)日志/数学:Eigen3.Gflags.Glog.Cere ...

  7. sublime python配置运行

    1.安装python环境 安装完成时,Win+R → 输入cmd → Enter → 调出来命令行,输入python确认安装是否成功. 2.安装sublime 3.打开sublime,选择工具--编译 ...

  8. C语言必踩神坑,世上本没有坑,摔的人多了,也就有了坑!

    这是一个伤心的故事,有多少小伙伴在写C语言代码时遇到过的情景! 提问:C语言中,未初始化的局部变量到底是多少? 答案往往是: 与编译器有关: 可能但不保证初始化为0: 未确定. 总之,全部都是些一本正 ...

  9. 第六章 IP基本原理

    一.引入 1.IP是网络层协议,也是当今应用最广泛的网络协议之一 2.IP协议规定了数据的封装方式,网络节点的标识方法,用于网络上数据的端到端的传递. 二.IP协议概述 1.IP及相关协议 2.IP的 ...

  10. react渲染数据3种方式

    直接渲染,()类似于模板字符串,包裹一个dom元素 ReactDOM.render( (<div> <h2>现在时间:{new Date().toLocaleTimeStrin ...