- 浏览: 174093 次
- 性别:
- 来自: 济南
文章分类
最新评论
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 =
[
['A','B','C','E'],
['S','F','C','S'],
['A','D','E','E']
]
word = "ABCCED", -> returns true,
word = "SEE", -> returns true,
word = "ABCB", -> returns false.
在一个二维的数组中找到一个目标单词。我们可以用深度优先搜索来解决,用递归来完成。用一个辅助的布尔型数组来记录当前元素是否已经被访问过。DFS的时候要注意检查边界情况,检查下标是否越界。用一个变量lth来记录当然查找到的可以匹配的长度,如果lth等于带匹配字符串的长度就返回true。代码如下:
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 =
[
['A','B','C','E'],
['S','F','C','S'],
['A','D','E','E']
]
word = "ABCCED", -> returns true,
word = "SEE", -> returns true,
word = "ABCB", -> returns false.
在一个二维的数组中找到一个目标单词。我们可以用深度优先搜索来解决,用递归来完成。用一个辅助的布尔型数组来记录当前元素是否已经被访问过。DFS的时候要注意检查边界情况,检查下标是否越界。用一个变量lth来记录当然查找到的可以匹配的长度,如果lth等于带匹配字符串的长度就返回true。代码如下:
public class Solution { public boolean exist(char[][] board, String word) { if(word == null || word.length() == 0) return true; else if(board == null || board.length == 0) return false; boolean[][] isVisited = 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)) { isVisited[i][j] = true; boolean top = DFS(i - 1, j, 1, word, board, isVisited); boolean bottom = DFS(i + 1, j, 1, word, board, isVisited); boolean left = DFS(i, j - 1, 1, word, board, isVisited); boolean right = DFS(i, j + 1, 1, word, board, isVisited); if(top || bottom || left || right) return true; else isVisited[i][j] = false; } } return false; } public boolean DFS(int i, int j, int lth, String word, char[][] board, boolean[][] isVisited) { if(word.length() == lth) return true; if(i < 0 || i == board.length || j < 0 || j == board[0].length) return false; if(!isVisited[i][j] && board[i][j] == word.charAt(lth)) { isVisited[i][j] = true; boolean top = DFS(i - 1, j, lth + 1, word, board, isVisited); boolean bottom = DFS(i + 1, j, lth + 1, word, board, isVisited); boolean left = DFS(i, j - 1, lth + 1, word, board, isVisited); boolean right = DFS(i, j + 1, lth + 1, word, board, isVisited); if(top || bottom || left || right) return true; else isVisited[i][j] = false; } return false; } }
发表评论
-
498. Diagonal Traverse
2019-11-15 13:52 228Given a matrix of M x N eleme ... -
496 Next Greater Element I
2019-11-14 13:50 231You are given two arrays (witho ... -
Word Break II
2016-03-09 03:15 344Given a string s and a dictiona ... -
Insert Interval
2016-03-08 02:11 336Given a set of non-overlapping ... -
Merge Intervals
2016-03-07 05:25 455Given a collection of intervals ... -
Merge k Sorted Lists
2016-03-07 04:03 519Merge k sorted linked lists and ... -
Multiply Strings
2016-03-06 07:27 434Given two numbers represented a ... -
N-Queens II
2016-03-06 03:06 623Follow up for N-Queens problem. ... -
N-Queens
2016-03-06 02:47 432The n-queens puzzle is the prob ... -
First Missing Positive
2016-03-05 03:09 389Given an unsorted integer array ... -
Spiral Matrix
2016-03-04 03:39 522Given a matrix of m x n element ... -
Trapping Rain Water
2016-03-04 02:54 537Given n non-negative integers r ... -
Repeated DNA Sequences
2016-03-03 03:10 374All DNA is composed of a series ... -
Increasing Triplet Subsequence
2016-03-02 02:48 866Given an unsorted array return ... -
Maximum Product of Word Lengths
2016-03-02 01:56 884Given a string array words, fin ... -
LRU Cache
2016-02-29 10:37 558Design and implement a data str ... -
Super Ugly Number
2016-02-29 07:07 604Write a program to find the nth ... -
Longest Increasing Path in a Matrix
2016-02-29 05:56 766Given an integer matrix, find t ... -
Coin Change
2016-02-29 04:39 738You are given coins of differen ... -
Minimum Height Trees
2016-02-29 04:11 632For a undirected graph with tre ...
相关推荐
word search puzzle,
This utility as designed to aid in the generation of a word search puzzle, which can be very time consuming to the puzzle creator from the point of layout and then finally randomly filling in the ...
用于文本批量更改,实现高速快捷,谢谢使用!
WordSearch.zip
wordsearch.py
wordsearch.exe
Wordsearch解算器Python 首先,它要求一个包含单词搜索的文本文件。 然后,它将数据导入到数组中。 用户输入他们要查找的单词,然后打印单词搜索并突出显示该单词,因此您可以清楚地看到其位置。 查找单词的算法...
WordSearch_java_源码
Word Search Game in Python Free Source Code.zip
C语言写的 Word Search Puzzle游戏,单词匹配游戏,生成字母矩阵,通过随机横向、纵向、斜向插入单词,用户进行单词寻找,在规定时间内找出所有单词则游戏通过。
Unity3d Word Search Cookies 3.5 英文单词拼写益智小游戏源码
Word Search Puzzle是一款有趣且易于使用的益智游戏,适用于您的浏览器。 该游戏的目标是在屏幕上制作带有随机字母的单词。 只需在您认为可能有一个有意义的词的地方画一条线即可。 如果是正确的猜测,则将标记该...
我第一次尝试使用可用的公共程序-C ++中的WordSearch创建者。 温柔一点吧!
wordsearch:单词搜索生成器
词搜索单词搜索益智游戏这是一个简单的Word搜索难题示例游戏,适用于iOS平台。 我作为最后的工作面试来做的,所以这只是一个示例,例如,字母网格是固定的,没有任何自动生成。 它可以在iPhone和iPad上使用。 版权...
英语词典搜索功能需要自己配置词库,搜索词库查词
#WordSearch星期三 Medium 提出了一个挑战,即在单词搜索中找到所有与写作相关的单词。 这个脚本是我找到所有隐藏单词的解决方案。 不幸的是,它发现了一些太多...... 此 repo 中包含用于遍历单词搜索和查找单词的...
Word Search Puzzle是一款有趣且易于使用的益智游戏,适用于您的浏览器。 该游戏的目标是在屏幕上制作带有随机字母的单词。 只需在您认为可能有一个有意义的词的地方画一条线即可。 如果是正确的猜测,则将标记该...
单词搜索引擎用Python编写的单词搜索项目。关于这是用于创建和解决单词搜索难题的引擎。 该生成器可以创建任意大小的网格,并且可以添加无限数量的单词而不会挂起,只要所有单词都可以符合指定的网格大小即可。...
Unity精品源码,高可用,Unity精品源码,高可用,Unity精品源码,高可用,Unity精品源码,高可用,Unity精品源码,高可用,Unity精品源码,高可用,Unity精品源码,高可用,Unity精品源码,高可用,Unity精品源码,...