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", -> returns true,
word = "SEE", -> returns true,
word = "ABCB", -> returns false.

解题思路:

开一个数组用于保存数据是否被访问过,使用DFS即可,JAVA实现如下:

static public boolean exist(char[][] board, String word) {
if (board.length == 0 || board[0].length == 0 || word.length() == 0)
return false;
boolean[][] isUsed=new boolean[board.length][board[0].length];
for (int i = 0; i < board.length; i++)
for (int j = 0; j < board[0].length; j++)
if (board[i][j] == word.charAt(0)){
isUsed[i][j]=true;
if(dfs(board,word,i,j,0,isUsed))
return true;
isUsed[i][j]=false;
}
return false;
}
public static boolean dfs(char[][] board,String word,int i,int j,int depth,boolean[][] isUsed){
if(depth==word.length()-1)
return true;
if(i>=1&&board[i-1][j]==word.charAt(depth+1)&&!isUsed[i-1][j]){
isUsed[i-1][j]=true;
if(dfs(board,word,i-1,j,depth+1,isUsed))
return true;
isUsed[i-1][j]=false;
}
if(i<=board.length-2&&board[i+1][j]==word.charAt(depth+1)&&!isUsed[i+1][j]){
isUsed[i+1][j]=true;
if(dfs(board,word,i+1,j,depth+1,isUsed))
return true;
isUsed[i+1][j]=false;
}
if(j>=1&&board[i][j-1]==word.charAt(depth+1)&&!isUsed[i][j-1]&&!isUsed[i][j-1]){
isUsed[i][j-1]=true;
if(dfs(board,word,i,j-1,depth+1,isUsed))
return true;
isUsed[i][j-1]=false;
}
if(j<=board[0].length-2&&board[i][j+1]==word.charAt(depth+1)&&!isUsed[i][j+1]){
isUsed[i][j+1]=true;
if(dfs(board,word,i,j+1,depth+1,isUsed))
return true;
isUsed[i][j+1]=false;
}
return false;
}

Java for LeetCode 079 Word Search的更多相关文章

  1. Java for LeetCode 212 Word Search II

    Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...

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

  3. [LeetCode] 212. Word Search II 词语搜索 II

    Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...

  4. Java for LeetCode 126 Word Ladder II 【HARD】

    Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from ...

  5. [LeetCode] 212. Word Search II 词语搜索之二

    Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...

  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 、212. Word Search II

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

  8. 【leetcode】Word Search

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

  9. [LeetCode]题解(python):079 Word Search

    题目来源 https://leetcode.com/problems/word-search/ Given a 2D board and a word, find if the word exists ...

随机推荐

  1. 【CodeForces 618B】Guess the Permutation

    题 题意 有个1到n的一个全排列,告诉你第i个数和全部n个数相比的较小的是哪个,和自己相比时为0,于是有个主对角线为0的矩阵,求原数列 分析 我的想法是,给我们的每一行之和按大小排一下,就知道第i个数 ...

  2. Yii2修改默认布局

    public $layout = 'layout';//在类中定义一个变量,名为$layout的php文件 <?php echo $content; ?>

  3. 【转】HTTP长连接与短连接

    1. HTTP协议与TCP/IP协议的关系 HTTP的长连接和短连接本质上是TCP长连接和短连接.HTTP属于应用层协议,在传输层使用TCP协议,在网络层使用IP协议.IP协议主要解决网络路由和寻址问 ...

  4. cogs896 圈奶牛

    描述 农夫约翰想要建造一个围栏用来围住他的奶牛,可是他资金匮乏.他建造的围栏必须包括他的奶牛喜欢吃草的所有地点.对于给出的这些地点的坐标,计算最短的能够围住这些点的围栏的长度. PROGRAM NAM ...

  5. 求DAG上两点的最短距离

    Problem 给出一个不带边权(即边权为1)的有向无环图(unweighted DAG)以及DAG上两点s, t,求s到t的最短距离,如果无法从s走到t,则输出-1. Solution DFS,BF ...

  6. Laravel 5 中的配置

    介绍 Laravel 的所有的配置文件都放在了 config 这个目录的下面.每个选项都有介绍. config├── app.php├── auth.php├── cache.php├── compi ...

  7. node-js访问rest api的方法

    //npm install node-rest-client --save-dev var Client = require('node-rest-client').Client function l ...

  8. VirtualBox安装debian的详细方法步骤

    下面是用VirtualBox安装Debian6的方法和步骤 l 新建一个文件夹,用于存放虚拟硬盘,如Debian l 打开VirtualBox,点击新建 l 输入虚拟机名称,Debian_6 l 给虚 ...

  9. 根据某列,将两个 dataframe 合并

    import pandas as pd import numpy as np df1 = pd.DataFrame(np.array([['a', 5, 9], ['b', 4, 61], ['c', ...

  10. ps 倒影制作

    首先打开PS并打开一张素材,这里我选择了山水图片,制作山峰在水中的倒影效果.   然后按下[Crrl+J]复制这个图层,如图:   接着按下[Ctrl+T]或者是[编辑][自由变换],打开[自由变换] ...