LeetCode 79. Word Search(单词搜索)
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.
题目标签:Array
Java Solution:
Runtime beats 34.54%
完成日期:07/27/2017
关键词:Array
关键点:Braktracking;新矩阵记录探索过的点;每个点有4个方向可以探索
public class Solution
{
public boolean exist(char[][] board, String word)
{
if(board == null || board.length == 0
|| board.length * board[0].length < word.length())
return false; boolean[][] mark = new boolean[board.length][board[0].length];
boolean res = false;
// iterate board, find the match starting character to pass to findWord function
for(int i=0; i<board.length; i++)
{
for(int j=0; j<board[0].length; j++)
{
if(board[i][j] == word.charAt(0))
res = res || findWord(board, word, 0, i, j, mark); if(res)
return res;
}
} return res;
} public boolean findWord(char[][] board, String word, int wordIndex,
int row, int col, boolean[][] markBoard)
{
// base case 1: if exceed word's length, meaning it is done and found the word
if(wordIndex == word.length())
return true; /* base case 2: if this character is out of bound or
* this character is not match to word's character or
* hits character has been already visited
*/
if(row >= board.length || row < 0 || col >= board[0].length || col < 0
|| word.charAt(wordIndex) != board[row][col] || markBoard[row][col])
return false; // mark this char as visited
markBoard[row][col] = true; // follow top, right, bottom, left order to check character
// if any direction future path return true, meaning no need to continue other directions
if(findWord(board, word, wordIndex + 1, row - 1, col, markBoard) || // go top
findWord(board, word, wordIndex + 1, row, col + 1, markBoard) || // go right
findWord(board, word, wordIndex + 1, row + 1, col, markBoard) || // go bottom:
findWord(board, word, wordIndex + 1, row, col - 1, markBoard)) // go left:
{
return true;
} markBoard[row][col] = false; // clear the mark of this character // if this this character's all four directions path has failed, return false to last level
return false;
}
}
参考资料:N/A
LeetCode 算法题目列表 - LeetCode Algorithms Questions List
LeetCode 79. Word Search(单词搜索)的更多相关文章
- [LeetCode] 79. 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 79. Word Search单词搜索 (C++)
题目: Given a 2D board and a word, find if the word exists in the grid. The word can be constructed fr ...
- [LeetCode] 79. 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 79. Word Search 、212. Word Search II
https://www.cnblogs.com/grandyang/p/4332313.html 在一个矩阵中能不能找到string的一条路径 这个题使用的是dfs.但这个题与number of is ...
- LeetCode 79 Word Search(单词查找)
题目链接:https://leetcode.com/problems/word-search/#/description 给出一个二维字符表,并给出一个String类型的单词,查找该单词是否出现在该二 ...
- [LeetCode OJ] Word Search 深度优先搜索DFS
Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from l ...
- 079 Word Search 单词搜索
给定一个二维面板和一个单词,找出该单词是否存在于网格中.这个词可由顺序相邻的单元格内的字母构成,其中“相邻”单元格是那些水平相邻或垂直相邻的单元格.同一个单元格内的字母不允许被重复使用.例如,给定 二 ...
- Leetcode79. Word Search单词搜索
给定一个二维网格和一个单词,找出该单词是否存在于网格中. 单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中"相邻"单元格是那些水平相邻或垂直相邻的单元格.同一个单元格内的字 ...
- Leetcode#79 Word Search
原题地址 依次枚举起始点,DFS+回溯 代码: bool dfs(vector<vector<char> > &board, int r, int c, string ...
随机推荐
- 多线程:多线程设计模式(三):Master-Worker模式
Master-Worker模式是常用的并行模式之一,它的核心思想是,系统有两个进程协作工作:Master进程,负责接收和分配任务:Worker进程,负责处理子任务.当Worker进程将子任务处理完成后 ...
- 全方位解读"CPU load average"
前一段时间,有同事因为“CPU负载到达5算不算高”的问题争论了一番,看似简单的一个问题表明了我们并没有真正理解服务器的CPU负载. 如果你的线上服务出现性能问题,那么检查机器的CPU负载情况是必不可少 ...
- C#中的两把双刃剑:抽象类和接口
问题出现: 这也是我在学习抽象类和接口的时候遇到的问题,从我归纳的这三个问题,不难看出这也许是我们大多数程序员遇到问题的三个阶段, 第一阶段(基础概念):就象问题1一样,这部分人首先需要扫清基础概念的 ...
- nodejs 初次链接 mongodb 的详细细节
时间 2016-06-2613:05:16 在前端的学习也有一段时间了,学习了html,css,javascript,jqery,ajax,php,mysql,学习了这些,了解了一些皮毛,也没有什么 ...
- Java多线程Runnable与Callable区别与拓展
我们先来分别看一下这两个接口 Runnable: // // Source code recreated from a .class file by IntelliJ IDEA // (powered ...
- Java中的HTTP通信技术详解
1.使用HTTP的Get方式读取网络数据 class ReadByGet extends Thread{ @Override public void run(){ URL url = n ...
- Message:Unable to locate element 问题解决方法
Python断断续续学了有一段时间了,总感觉不找个小项目练练手心里没底,哪成想出门就遇到"拦路虎",一个脚本刚写完就运行报错,还好做足了心里准备,尝试自行解决. 或许网上有相关解决 ...
- HDU 5976 数学
Detachment Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total ...
- 如何维护一个1000 IP的免费代理池
楔子 好友李博士要买房了, 前几天应邀帮他抓链家的数据分析下房价, 爬到一半遇到了验证码. 李博士的想法是每天把链家在售的二手房数据都抓一遍, 然后按照时间序列分析. 链家线上在交易的二手房数据大概有 ...
- 基于LoadRunner11,以wifi热点方式录制APP脚本简单指导
本想详细写下操作过程,但并不觉着十分必要,通过baidu或我要自学网均能找到相关资料,所以详细操作过程不再赘述,只是把过程中遇到的问题说明下解释下,让大家“录制APP”的路更平坦! 1.如何使用Loa ...