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 ...
随机推荐
- GCD之全局、主线程
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 -(NSString *)fetchData { [NSThread sleepFo ...
- epoll的ET和LT两种模式对比及注意事项
ET模式: 因为ET模式只有从unavailable到available才会触发,所以 1.读事件:需要使用while循环读取完,一般是读到EAGAIN,也可以读到返回值小于缓冲区大小: 如果应用层读 ...
- “一切都是消息”--MSF(消息服务框架)入门简介
“一切都是消息”--这是MSF(消息服务框架)的设计哲学. MSF的名字是 Message Service Framework 的简称,中文名称:消息服务框架,它是PDF.NET框架的一部分. 1,M ...
- 关于js浮点数计算精度不准确问题的解决办法
今天在计算商品价格的时候再次遇到js浮点数计算出现误差的问题,以前就一直碰到这个问题,都是简单的使用tofixed方法进行处理一下,这对于一个程序员来说是及其不严谨的.因此在网上收集了一些处理浮点数精 ...
- ADALINE模型
ADALINE模型即自适应线性单元(Adaptive Linear Neuron),主要用于信号处理中的自适应滤波.预测和模式识别.其结构图如下 输入向量X=(x0,x1,x2,...,xn)T每个输 ...
- D. How many trees? DP
D. How many trees? time limit per test 1 second memory limit per test 64 megabytes input standard in ...
- ajax url参数中文乱码解决方法
较好的处理办法,对js的url中的中文参数值使用两次encodeURI(),即encodeURI(encodeURI("url的中文参数值")) JS代码: var name=&q ...
- DNS—正、反向解析;委派;主从;子域;转发;智能dns等的实现
前言:DNS,耳熟能详的东西,内容太多,小编也不太好讲清,只能写几个实验详解,供大家参考. 一.简单介绍 1.DNS:通过主机名,最终得到该主机名对应的IP地址的过程叫做域名解析(或主机名解析). 端 ...
- Jquery字符串,数组(拷贝、删选、合并等),each循环,阻止冒泡,ajax出错,$.grep筛选,$.param序列化,$.when
<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- $http设置headers来实现IE不缓存url请求的资源
var getOrders = function(){ var deferred = $q.defer(); $http({ method:'get', url:'/getOr ...