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 ...
随机推荐
- mysql 中 LIMIT的简单用法
mysql--语法: SELECT * FROM table LIMIT [offset,] rows | rows OFFSET offset --举例: select * from table l ...
- Find Successor & Predecessor in BST
First, we use recursive way. Successor public class Solution { public TreeNode inorderSuccessor(Tree ...
- 【HDU1754】I Hate It(线段树)
update:单点替换 query:区间最值 #include <iostream> #include <cstring> #include <cstdlib> # ...
- DBA 经典面试题(5)
国外公司的Oracle DBA试题 Oracle DBA Interview Questions 1. How many memory layers are in the shared pool? 2 ...
- ios 以NSObject为父类的各类间继承关系
- 《Swift开发指南》国内第一本Swift图书上市了
<Swift开发指南>国内第一本Swift图书上市了 既<courseId=799262">苹果Swift编程语言开发指南>视频教程地址:courseId=79 ...
- [android开发之内容更新类APP]二、这几日的结果
android教程即将開始 话说这开了blog之后,就一直在试用自己的app,发现.TM的真的非常不爽,不好用,好吧.本来打算放弃了.只是看到手机里还有还有一个坑,干脆又一次做一个吧. 原来的神回复A ...
- Audio笔记之MediaPlayerService:setDataSource
//下面是一个典型的播放序列: MediaPlayer player=new MediaPlayer() player->setDataSource(url,header); player-&g ...
- Android Training: 设备管理
Android 设备管理 Android2.2 通过Android设备管理API提供对企业级应用的支持.设备管理API在系统级别提供了设备管理特性.这些API可以在企业环境下,需要对员工设备进行控制时 ...
- HBase配置&启动脚本分析
本文档基于hbase-0.96.1.1-cdh5.0.2,对HBase配置&启动脚本进行分析 date:2016/8/4 author:wangxl HBase配置&启动脚本分析 剔除 ...