给定一个二维面板和一个单词,找出该单词是否存在于网格中。
这个词可由顺序相邻的单元格内的字母构成,其中“相邻”单元格是那些水平相邻或垂直相邻的单元格。同一个单元格内的字母不允许被重复使用。
例如,
给定 二维面板 =
[
  ['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. codeforces 703D D. Mishka and Interesting sum(树状数组)

    题目链接: D. Mishka and Interesting sum time limit per test 3.5 seconds memory limit per test 256 megaby ...

  2. ACM学习历程—FZU 2144 Shooting Game(计算几何 && 贪心 && 排序)

    Description Fat brother and Maze are playing a kind of special (hentai) game in the playground. (May ...

  3. bzoj 2067 [Poi2004]SZN——二分+贪心

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2067 最少的线段可以贪心地想出来.(结果还是写错了)就是偶数孩子可以自己配对,奇数孩子要带一 ...

  4. 利用MEF实现插件机制(可根据输入类型来加载特定dll)

    最近在做PACS的项目中想利用插件来加载各个不同的SCP的操作实现.比如Worklist的查询数据库,可以有多个实现. 比如MPPS的更新,也可以有多个实现. 为了统一弹性处理插件模块,增加了类型输入 ...

  5. ubuntu下caffe配置

    http://blog.csdn.net/yhaolpz/article/details/71375762

  6. Umbraco image中使用Crop URL

    需要在Umbraco 的image中使用crop URL.首先你需要取出这个image作为IPublishedContent 有以下两种方法 第一种: var imageId = Model.Cont ...

  7. Linux下的eclipse的安装

    其实在linux里配好java环境后,就已经好了. 从官网下一个eclipse,解压后,就可以直接执行eclipse了,实在是不知道什么是安装. 其实在配置java时,也是一样. 可见在linux下, ...

  8. Unity手游引擎安全解析及实践

    近日,由Unity主办的"Unity技术开放日"在广州成功举办,网易移动安全技术专家卓辉作为特邀嘉宾同现场400名游戏开发者分享了网易在手游安全所积累的经验.当下,很多手游背后都存 ...

  9. 51nod1100(计算斜率)

    题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1100 题意:中文题啦- 思路:算斜率不用多说吧?本题唯一一个 ...

  10. 2014-10-5 NOIP模拟赛

    祖孙询问 (tree.pas/c/cpp) [问题描述] 已知一棵n个节点的有根树.有m个询问.每个询问给出了一对节点的编号x和y,询问x与y的祖孙关系. [输入格式] 输入第一行包括一个整数n表示节 ...