题目链接:https://leetcode.com/problems/word-search/#/description

给出一个二维字符表,并给出一个String类型的单词,查找该单词是否出现在该二维字符表中。

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.

同样还是使用第78题的方法,也就是回溯法求解。

通过上述例子,每次都是只能查找当前位置的上下左右四个位置(最多是四个位置,还有边界情况)。当查找到该单词的某个位置时,通过计数器不断更新当前已经匹配的字符的个数。

通过移动查找的坐标(row,col)来进行对一棵树的深度遍历操作。也就是dfs遍历操作。

调用dfs遍历的循环是有两层的。每次当遍历的一棵子树到叶子节点时,重新进入原始的循环体。更新树的遍历根节点。

因此循环体为

for(int i = 0 ; i < row ; i++){
for(int j = 0 ; j < col ; j++){
dfs();// calling the function to go through the tree
}
}

需要考虑递归函数的出口问题:因为需要对单词中的每个字符进行匹配操作,所以设置计数器用来统计当前所匹配的字符个数,同时也可以使用该变量来得到接下来要匹配的单词的字符。

如果给计数器个数等于单词的长度时,说明单词的所有字符都可以在表中找到,此时返回结果为true

如果当前的位置上下左右四个cell都不能与单词的字符进行匹配时,此时递归函数应退出并放回结果为false;

上面考虑了递归函数的出口问题,下面是对整个问题的结果进行讨论。

当有一个位置能够查找到目标单词的所有字符时就应该返回true,

当遍历开始位置从(0,0)到(board.row, board.col)结束都没有查找到时,返回false;

根据上述分析,得到以下参考代码:

package leetcode_100;

/***
*
* @author pengfei_zheng
* 二维字符表中查找单词
*/
public class Solution79 {
public static boolean exist(char[][] board, String word) {
int row = board.length;
int col = board[0].length;
boolean[][] visited = new boolean[row][col];//record whether this cell is visited
for(int i=0;i<row;i++)
for(int j=0;j<col;j++)
if(dfs(board,visited,i,j,0,word))// calling the function to check word
return true;//if this start position(i,j) can match the word then just return true return false;//if all the position can't match the word then return false
} private static boolean dfs(char[][] board, boolean[][] visited, int row, int col, int index, String word) {
if(word.length() == index){//judge whether match the word
return true;
}
if(row<0 || col<0|| row>=board.length || col>=board[0].length) return false;//considering the boundary
char ch = word.charAt(index);//get next Character
if(!visited[row][col] && ch == board[row][col]){// this position can match the Character
visited[row][col] = true;
//calling itself going through the tree
//four conditions: up && down && left && right
boolean res = dfs(board,visited,row-1,col,index+1,word)|| dfs(board,visited,row+1,col,index+1,word)
||dfs(board,visited,row,col-1,index+1,word)|| dfs(board,visited,row,col+1,index+1,word);
visited[row][col] = false;
return res;
}
return false;//not found
}
}
 

LeetCode 79 Word Search(单词查找)的更多相关文章

  1. [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 ...

  2. 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 ...

  3. [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 ...

  4. leetcode 79. Word Search 、212. Word Search II

    https://www.cnblogs.com/grandyang/p/4332313.html 在一个矩阵中能不能找到string的一条路径 这个题使用的是dfs.但这个题与number of is ...

  5. 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 ...

  6. Leetcode#79 Word Search

    原题地址 依次枚举起始点,DFS+回溯 代码: bool dfs(vector<vector<char> > &board, int r, int c, string ...

  7. [LeetCode] 212. Word Search II 词语搜索 II

    Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...

  8. 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 ...

  9. 刷题79. Word Search

    一.题目说明 题目79. Word Search,给定一个由字符组成的矩阵,从矩阵中查找一个字符串是否存在.可以连续横.纵找.不能重复使用,难度是Medium. 二.我的解答 惭愧,我写了很久总是有问 ...

随机推荐

  1. oracle装载表是什么?

    oracle装载表即通过sqlloader的方式导入数据. Oracle 的SQL*LOADER可以将外部数据加载到数据库表中.下面是SQL*LOADER的基本特点: 1)能装入不同数据类型文件及多个 ...

  2. IE中自定义标签使用自封闭格式引发错误!

    最近学习IONIC,其中用到了ion-menu-nav-button,由于标签开始和结尾之间没有内容,所以图省事儿使用自封闭标签的写法: <ion-menu-nav-button class=& ...

  3. POI初体验

    Apache POI是Apache软件基金会的开放源码函式库,POI提供API给Java程序对Microsoft Office格式档案读和写的功能. 它的结构如下: HSSF - 提供读写Micros ...

  4. Java初学者必学的JSTL

    所谓JSTL就是标签库  JSP Standard Tag Library,如果做为java初学者你看不懂那些$符号的话,就有必要来了解一下JSTL,如果你看到满眼的<%}%>(Scrip ...

  5. 1 salt执行模块开发

    saltstack自带的模块已经很多了,但是有些时候我们需要自己开发出自己的模块来满足自己的需求,那就要自己开发了,下面请看例子 创建_modules目录在file_roots下面 我们需要在file ...

  6. .net的session详解 存储模式 存到数据库中 使用范围与大小限制 生命周期

    Session又称为会话状态,是Web系统中最常用的状态,用于维护和当前浏览器实例相关的一些信息.举个例子来说,我们可以把已登录用户的用户名放在Session中,这样就能通过判断Session中的某个 ...

  7. Cannot call sendError() after the response has been committed - baiyangliu

    当response提交后,不能调用sendError(),什么意思? 出现这个错误,一定是多次response导致的.可以这么理解,承载客户端和服务器进行Http交互的Socket连接已经关闭了,而你 ...

  8. 【转】细谈Redis和Memcached的区别

    Redis的作者Salvatore Sanfilippo曾经对这两种基于内存的数据存储系统进行过比较: Redis支持服务器端的数据操作:Redis相比Memcached来说,拥有更多的数据结构和并支 ...

  9. js提取新浪邮箱的信用卡

    js提取用户新浪邮箱中的信用卡信息,是js非nodejs. 对比py,之前就做不好,出现了复杂点选验证码.js的开发速度只需要py的三分之一,甚至十分之一. js在客户端执行,py在后端执行,py要实 ...

  10. NYOJ 116 士兵杀敌 (线段树,区间和)

    题目链接:NYOJ 116 士兵杀敌 士兵杀敌(二) 时间限制:1000 ms  |  内存限制:65535 KB 难度:5 描写叙述 南将军手下有N个士兵,分别编号1到N,这些士兵的杀敌数都是已知的 ...