给定一个二维面板和一个单词,找出该单词是否存在于网格中。
这个词可由顺序相邻的单元格内的字母构成,其中“相邻”单元格是那些水平相邻或垂直相邻的单元格。同一个单元格内的字母不允许被重复使用。
例如,
给定 二维面板 =
[
  ['A','B','C','E'],
  ['S','F','C','S'],
  ['A','D','E','E']
]
单词= "ABCCED",-> 返回 true,
单词 = "SEE", -> 返回 true,
单词 = "ABCB", -> 返回 false。
详见:https://leetcode.com/problems/word-search/description/

Java实现:

class Solution {
public boolean exist(char[][] board, String word) {
int m = board.length;
int n = board[0].length;
boolean[][] visited = new boolean[m][n];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (dfs(board, word, 0, i, j, visited)) {
return true;
}
}
}
return false;
} public boolean dfs(char[][] board, String word, int index, int rowindex, int colindex, boolean[][] visited) {
if (index == word.length()){
return true;
}
if (rowindex < 0 || colindex < 0 || rowindex >=board.length || colindex >= board[0].length){
return false;
}
if (visited[rowindex][colindex]){
return false;
}
if (board[rowindex][colindex] != word.charAt(index)){
return false;
}
visited[rowindex][colindex] = true;
boolean res =
dfs(board, word, index + 1, rowindex - 1, colindex, visited)
|| dfs(board, word, index + 1, rowindex + 1, colindex, visited)
|| dfs(board, word, index + 1, rowindex, colindex + 1, visited)
|| dfs(board, word, index + 1, rowindex, colindex - 1, visited);
visited[rowindex][colindex] = false;
return res;
}
}

参考:https://www.cnblogs.com/springfor/p/3883942.html

079 Word Search 单词搜索的更多相关文章

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

  2. LeetCode 79. Word Search单词搜索 (C++)

    题目: Given a 2D board and a word, find if the word exists in the grid. The word can be constructed fr ...

  3. Leetcode79. Word Search单词搜索

    给定一个二维网格和一个单词,找出该单词是否存在于网格中. 单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中"相邻"单元格是那些水平相邻或垂直相邻的单元格.同一个单元格内的字 ...

  4. [LeetCode] Word Search 词语搜索

    Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from l ...

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

  6. [Leetcode] 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 OJ] Word Search 深度优先搜索DFS

    Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from l ...

  8. Java for LeetCode 079 Word Search

    Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from l ...

  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. python基础-大杂烩

    random()随机函数 import random print(random.choice('abcdefghij')) #随机取这些字母 print(random.choice(['apple', ...

  2. oracle数据库复习(1)

    数据库中的专业术语: 表:在数据库中存放数据所用的表 视图:数据库中的虚拟表.在视图中存放的是从数据表中查询出来的纪录 存储过程:存储过程是由SQL语句和控制流语句组成的代码块.存储过程在开发软件时, ...

  3. 关于JDK安装javac失效的几个问题。

    1.按照指南一步一步配置环境变量. 打开cmd,测试. 2.如果还是没有用,注意你的JAVA_HOME配置的是用户变量还是系统变量,改成系统变量. 打开cmd,测试. 3.如果还是没有用,不要你的JA ...

  4. 1095 Cars on Campus (30)(30 分)

    Zhejiang University has 6 campuses and a lot of gates. From each gate we can collect the in/out time ...

  5. bzoj 3680(洛谷1337) 吊打XXX——模拟退火

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3680 https://www.luogu.org/problemnew/show/P1337 ...

  6. hdu3501Calculation 2——欧拉函数模板

    题目: Problem Description Given a positive integer N, your task is to calculate the sum of the positiv ...

  7. poj1179多边形——区间DP

    题目:http://poj.org/problem?id=1179 区间DP,值得注意的是有负值,而且有乘法,因此可能会影响最大值: 注意memset中写-1仅仅是-1,-2才是一个很小的负数: 最后 ...

  8. 【VS工程设置】 编译动态库,命令行添加参数,不使用预编译头,指定该项目链接 哪种 运行库

    编译动态库 注意: 动态库: [目标文件扩展] => .dll + [配置类型] => 动态库(.dll) 静态库: [目标文件扩展] => .lib + [ 配置类型]=> ...

  9. Altium Designer如何从已有的PCB图中导出封装库

    1.打开PCB文件 2.选择  Design -> Make Integrated Library  (生成集成库) 注意,一定要在PCB 文件下  生成集成库!! 最终生成这个文件,打开这个文 ...

  10. asp.net mvc 注册中的邮箱激活功能实现

    基本流程图 注册页面就不再写出,现在将发送邮件的代码粘贴出来   public ActionResult SendEmial() { ; string validataCode = System.Gu ...