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

  前两天都没有刷题,都在总结反省找工作失败的原因。因为现在租的房子也快到期了,就开始寻觅新的居所。突然之间,在昨天看完家附近的一个apartment之后,想到,既然也要搬家,为何不搬去一个更适合找工作,而且房租更便宜的地方呢! (主要还是因为穷!)回到家之后,调查研究一天,决定了!准备搬家到其他州,毕竟现在居住的地方(纽约长岛),不是非常适合new grad 找工作(主要是自己也不是大神,竞争不过别人)而且消费又高。巧的是,昨天虽然没刷题,但是上来看了一眼visits记录,发现居然有一个新的美国地区访问,就是我要去的地方(德州),这也算是缘分吧,闲话就扯到这里,开始继续说题。
 
  这道题目给了我们一个矩阵,让我们找到矩阵中是否存在一个word,但是这个word是需要左右上下连起来的。换一种说法,就是在这个board上走path, 能不能找到这个word的path。一般题目做多了,这种要探究所有可能性的,特别是要倒退回来继续探索其他方向的,基本大多都是backtracking。那么我们要走path,首先要找到起点,先遍历board,把和word的第一个字母一样的都当作起点代入我们的递归function。我们要记录每一个点的position,所以要row 和col;我们要记录word中的character,所以要一个wordIndex;最重要的是,我们需要判断,哪些点我们已经走过了,所以要一个boolean markBoard[][];对于起点(每一个点),最基本的思路是,可以走四个方向,把那个方向的下一个点继续递归,只要有一条path 成功了,返回回来就可以直接return了,不需要继续把所有的pathes 都看完;对于每一个点,要检查base case 1, word 是否已经找到了 return true; 还要检查base case 2,这个点是否可以探索(比如这个点超出board范围了,它和word中的char不相等,它已经被探索过了), return false。
 
 
 

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(单词搜索)的更多相关文章

  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(单词查找)

    题目链接:https://leetcode.com/problems/word-search/#/description 给出一个二维字符表,并给出一个String类型的单词,查找该单词是否出现在该二 ...

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

  7. 079 Word Search 单词搜索

    给定一个二维面板和一个单词,找出该单词是否存在于网格中.这个词可由顺序相邻的单元格内的字母构成,其中“相邻”单元格是那些水平相邻或垂直相邻的单元格.同一个单元格内的字母不允许被重复使用.例如,给定 二 ...

  8. Leetcode79. Word Search单词搜索

    给定一个二维网格和一个单词,找出该单词是否存在于网格中. 单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中"相邻"单元格是那些水平相邻或垂直相邻的单元格.同一个单元格内的字 ...

  9. Leetcode#79 Word Search

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

随机推荐

  1. Shiro第三篇【授权、自定义reaml授权】

    Shiro授权 上一篇我们已经讲解了Shiro的认证相关的知识了,现在我们来弄Shiro的授权 Shiro授权的流程和认证的流程其实是差不多的: Shiro支持的授权方式 Shiro支持的授权方式有三 ...

  2. 如何在Oracle官网下载历史版本JDK

    打开Oracle官网,准备下载java JDK(下载时需要使用注册用户登陆,可以免费注册) 官网地址:http://www.oracle.com/ 2 鼠标悬停Downloads,会出现相关内容,如下 ...

  3. XML的序列化(Serializer)

    步骤: //1获取XmlSerializer 类的实例 通过Xml这个工具类去获取 XmlSerializer xmlSerializer = Xml.newSerializer(); try { / ...

  4. Angularjs –– Expressions(表达式)

    点击查看AngularJS系列目录 转载请注明出处:http://www.cnblogs.com/leosx/ Angular的表达式 Angular的表达式和JavaScript代码很像,不过通常A ...

  5. 一、js的数据类型

    一.数据类型 ECMAScript中有5种简单数据类型:Undefined.Null.Boolean.Number和String.还有一种复杂数据类型--Object.ECMAScript不支持任何创 ...

  6. 开博近一年的感想 by 程序员小白

    /* 好吧,这里的写博客应该理解为更宏观的写文章. */   在去年的这个时候,我所知道的平台只有 CSDN 和博客园..然而 CSDN 的广告实在是不想吐槽了,选择博客园是一件非常自然的事情.要说开 ...

  7. 学习Matplotlib

    介绍 Matplotlib是一个Python 2D绘图库,可以跨平台生成各种硬拷贝格式和交互式环境的出版品质量图.Matplotlib可用于Python脚本,Python和IPythonshell,j ...

  8. 使用docker部署standalone cinder

    | 版权:本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接.如有问题,可以邮件:wangxu198709@gmail.com 背景 OpenSta ...

  9. 决策树模型组合之随机森林与GBDT

    版权声明: 本文由LeftNotEasy发布于http://leftnoteasy.cnblogs.com, 本文可以被全部的转载或者部分使用,但请注明出处,如果有问题,请联系wheeleast@gm ...

  10. Java面向对象 网络编程 上

     Java面向对象 网络编程 上 知识概要:                     (1)网络模型 (2)网络通讯要素 (3)UDP TCP 概念 (4)Socket (5)UDP TCP 传输 ...