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 ...
随机推荐
- 应用程序打包(ipa)
如果想让用户可以安装ipa, 必须在打包程序的时候说清楚哪一个应用程序(appid)可以安装到哪一台设备上.(UDID). 原理: 要想打包, 告诉苹果, 哪一台电脑可以进行打包 步骤: 让电脑端具备 ...
- Linux系统启动流程(2)
内核设计风格: RedHat, SUSE核心:动态加载 内核模块内核:/lib/modules/“内核版本号命令的目录”/vmlinuz-2.6.32/lib/modules/2.6.32/ RedH ...
- Android——SQLite实现面向对象CRUD
android中SQLite的使用,事实上倒也不难.可是与JDBC操作数据库相比,这个还是有点不顺手,并且我好久没写底层的封装了,使用SSM框架这些都不须要考虑......好了,废话不多说.以下直接建 ...
- [Hapi.js] Up and running
hapi is a rock solid server framework for Node.js. Its focus on modularity and configuration-over-co ...
- NPOI通过DataTable导出和读取Excel
Excel导入及导出问题产生: 从接触.net到现在一直在维护一个DataTable导出到Excel的类,时不时还会维护一个导入类.以下是时不时就会出现的问题: 导出问题: 如果是asp.net,你得 ...
- javaScript增加样式规则(新增样式)
<html> <head> <link rel="stylesheet" type="text/css" href="b ...
- Gengxin讲STL系列——Set
本系列第二篇blog 第一篇写的心潮澎湃,结果写完一看,这都是些什么玩意= =| Set的中文名称是“集合”.集合,高一数学必修一课本给出的定义已经很明确了,简单来讲就是一个不含重复元素的空间(个人定 ...
- CDZSC_2015寒假新人(1)——基础 i
Description “Point, point, life of student!” This is a ballad(歌谣)well known in colleges, and you mus ...
- Reading source code
software is a system built up of many parts rebuild that decomposition see the patterns in codes is ...
- jxl读和取Excel文件
请参看下面链接: jxl如何读和取excle中的数据