Word Search 解答
Question
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
.
Solution
Because we have four choices to go, so this problem is not suitable to be solved by DP.
We use DFS here to traverse all possible paths.
One tricky note here is to check whether target equals to board[i][j] at every begining.
public class Solution {
public int[][] directions = {{0, 1},{0, -1},{1, 0},{-1, 0}}; public boolean exist(char[][] board, String word) {
int m = board.length, n = board[0].length;
boolean[][] visited = new boolean[m][n];
for (int i = 0; i < m; i++)
Arrays.fill(visited[i], false);
char target = word.charAt(0);
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (dfsSearch(board, word, 0, visited, i, j))
return true;
}
}
return false;
} private boolean dfsSearch(char[][] board, String word, int index, boolean[][] visited, int startX, int startY) {
if (index >= word.length())
return true;
int m = board.length, n = board[0].length;
if (startX < 0 || startX >= m || startY < 0 || startY >= n)
return false;
char target = word.charAt(index);
if (board[startX][startY] != target)
return false;
if (visited[startX][startY])
return false;
visited[startX][startY] = true; // Traverse four directions
boolean result = dfsSearch(board, word, index + 1, visited, startX, startY + 1) ||
dfsSearch(board, word, index + 1, visited, startX, startY - 1) ||
dfsSearch(board, word, index + 1, visited, startX + 1, startY) ||
dfsSearch(board, word, index + 1, visited, startX - 1, startY);
// Reset visited[startX][startY]
visited[startX][startY] = false;
return result;
}
}
Word Search 解答的更多相关文章
- 刷题79. Word Search
一.题目说明 题目79. Word Search,给定一个由字符组成的矩阵,从矩阵中查找一个字符串是否存在.可以连续横.纵找.不能重复使用,难度是Medium. 二.我的解答 惭愧,我写了很久总是有问 ...
- [LeetCode] Word Search II 词语搜索之二
Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...
- [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 ...
- Leetcode: word search
July 6, 2015 Problem statement: Word Search Given a 2D board and a word, find if the word exists in ...
- Word Search I & II
Word Search I Given a 2D board and a word, find if the word exists in the grid. The word can be cons ...
- 【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 ...
- 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 ...
- 51. Word Search
Word Search Given a 2D board and a word, find if the word exists in the grid. The word can be constr ...
- 79. 212. Word Search *HARD* -- 字符矩阵中查找单词
79. Word Search Given a 2D board and a word, find if the word exists in the grid. The word can be co ...
随机推荐
- Paint House 解答
Question There are a row of n houses, each house can be painted with one of the three colors: red, b ...
- JQuery 选择器 *很重要 多记
1)基本选择器: 跟CSS选择器类似 2) 层次选择器 div>span 紧接这div同一级下的全部span .one+div 同一等级的div #two~div 同一等级di ...
- SoupUI接口测试学习分享
一.SoapUI的使用 我们主要用SoapUI的REST 测试功能来测试我们协议接口.RESTful是一种服务端API的规范,每个资源对应唯一的URI,然后用HTTP的POST.GET.PUT.DEL ...
- hadoop执行hbase插入表操作,出错:Stack trace: ExitCodeException exitCode=1:(xjl456852原创)
在执行hbase和mapreduce融合时,将hdfs上的文本文件插入到hbase中,我没有使用"胖包"(胖包就是将项目依赖的jar包放入项目打包后的lib目录中),而是直接将hb ...
- pyqt MainWindow记录内容
class Texts(QtGui.QMainWindow,Ui_MainWindow): def __init__(self,parne=None): super(Texts,self).__ini ...
- robots.txt网站爬虫文件设置
目录: 什么是robots.txt robots.txt使用误区 robots.txt使用技巧 什么是robots.txt? robots.txt是搜索引擎中访问网站的时候要查看的第一个文件.Robo ...
- sql语句中查询出的数据添加一列,并且添加默认值
查询出数据,并且要添加一列表中都不存在的数据,且这一列的值都是相等的 select app_id,app_secret from wx_ticket group by app_id; 查询出的数据是 ...
- JSTL学习笔记(核心标签)
一.JSTL标签分类: 核心标签 格式化标签 SQL标签 XML标签 JSTL函数 二.核心标签 引用方式:<%@ taglib prefix="c" uri=& ...
- Visual Studio .NET、.NET Framework和C#之间的联系
Visual Studio .NET是一种集成开发环境(IDE),它包含3种高级程序设计语言,C#就是其中的一种:Visual Studio .NET之所以能把这三种语言有机结合起来并具有与平台无关的 ...
- uva 10391 Compound Words <set>
Compound Words You are to find all the two-word compound words in a dictionary. A two-word compound ...