题目描述

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

    sample input 5 6 7 2 3 1 1 5 0 6 0 0 8 6 6 5 3 4 3 7 8 2 4 0 0 6 9 sample output 20 样例解释: 分别以(2,1)为左 ...

  2. 使用 .NET 进行游戏开发

    微软是一家综合性的网络公司,相信这点来说不用过多的赘述,没有人不知道微软这个公司,这些年因为游戏市场的回报,微软收购了很多的游戏公司还有独立工作室,MC我的世界就是最成功的的案例,现在市值是排在全世界 ...

  3. PyCharm 上安装 Package(以 pandas 为例)

    一.使用 PyCharm 软件安装 pandas 包 1.打开 PyCharm 2.点击右上角 "Files" →"Settings..." 3.弹出" ...

  4. C&C++代码单元集成测试培训

    课程简介 本课程为期3天,结合实例讲解如何使用Cantata开展C和C++代码,通过培训,可以明显提高工程师操作Cantata的效率,并加速单元测试和集成测试. [日期]2020年11月3日-5日(共 ...

  5. openstack 高可用环境部署(8节点)(一)

  6. centos8:linux平台查看线程(ps/pstree/top)

    一,ps/pstree/top命令所属的rpm包 pstree所属的包 [root@blog ~]# whereis pstree pstree: /usr/bin/pstree /usr/bin/p ...

  7. volatile与重排序

    使用关键字volatile可以禁止代码的重排序: 在Java程序运行时,JIT(即使编译器)可以动态地改变程序代码运行地顺序:例如,有如下代码: A代码-重耗时 B代码-轻耗时 C代码-重耗时 D代码 ...

  8. maven项目导入并运行

    idea导入maven工程流程 找到要导入的文件位置 打开导入 选择manve 一直next就好 选择jdk,选择自己的jdk--home就可以 点击finished 等待坐标导入,查看右侧maven ...

  9. 图的全部实现(邻接矩阵 邻接表 BFS DFS 最小生成树 最短路径等)

    1 /** 2 * C: Dijkstra算法获取最短路径(邻接矩阵) 3 * 6 */ 7 8 #include <stdio.h> 9 #include <stdlib.h> ...

  10. 支持向量机(SVM)必备概念(凸集和凸函数,凸优化问题,软间隔,核函数,拉格朗日乘子法,对偶问题,slater条件、KKT条件)

    SVM目前被认为是最好的现成的分类器,SVM整个原理的推导过程也很是复杂啊,其中涉及到很多概念,如:凸集和凸函数,凸优化问题,软间隔,核函数,拉格朗日乘子法,对偶问题,slater条件.KKT条件还有 ...