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 ...
随机推荐
- libeXosip2(1-2) -- How-To initiate, modify or terminate calls.
How-To initiate, modify or terminate calls. The eXtented eXosip stack eXosip2 offers a flexible API ...
- Java反编译插件jad
原文地址:http://www.cnblogs.com/JimLy-BUG/p/5405868.html 1.首先下载jar文件:net.sf.jadclipse_3.3.0.jar 下载 2. ...
- poj 3579 Median (二分搜索之查找第k大的值)
Description Given N numbers, X1, X2, ... , XN, let us calculate the difference of every pair of numb ...
- Linux各种包安装及命令
1.Locate yum -y install mlocate 若出现问题: locate: can not stat () `/var/lib/mlocate/mlocate.db': 没有那个文件 ...
- PC--CSS技巧
1.图片不存在的时候,显示一个默认图片 <img src=”01.jpg” onerror=”this.src=’02.jpg'” /> 2.CSS强制图片自适应大小 img {width ...
- Spark MLlib Deep Learning Deep Belief Network (深度学习-深度信念网络)2.3
Spark MLlib Deep Learning Deep Belief Network (深度学习-深度信念网络)2.3 http://blog.csdn.net/sunbow0 第二章Deep ...
- 黑马程序猿 IO流 ByteArrayInputStream与ByteArrayOutputStream
---------------------- ASP.Net+Unity开发..Net培训.期待与您交流! ---------------------- package cn.itcast.IO; i ...
- C#高性能大容量SOCKET并发(十一):编写上传client
client封装总体框架 client编程基于堵塞同步模式,仅仅有数据正常发送或接收才返回,假设错误发生则抛出异常,基于TcpClient进行封装,主要类结构例如以下图: TcpClient:NET系 ...
- 改进的简单Tooltips显示
使用js简单改进了Tooltips的显示效果,可进一步使用CSS对改进的Tooltips进行美化. 前台布局代码: <asp:Panel ID="Panel1" runat= ...
- 第二章 Android Studio使用第三方模拟器
1.为什么要使用第三方模拟器 Android Studio自带模拟器,相对Eclipse来说项目启动速度的确快了很多倍,提高了开发效率.但和第三方模拟器进行对比的话,还是第三方的模拟器运行速度更快些. ...