题目

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.

题解

这道题分析看,就是一个词,在一行出现也是true,一列出现也是true,一行往下拐弯也是true,一行往上拐弯也是true,一列往左拐弯也是true,一列往右拐弯也是true。所以是要考虑到所有可能性,基本思路是使用DFS来对一个起点字母上下左右搜索,看是不是含有给定的Word。还要维护一个visited数组,表示从当前这个元素是否已经被访问过了,过了这一轮visited要回false,因为对于下一个元素,当前这个元素也应该是可以被访问的。

代码如下:

 1     public boolean exist(char[][] board, String word) {
 2         int m = board.length;  
 3         int n = board[0].length;  
 4         boolean[][] visited = new boolean[m][n];  
 5         for (int i = 0; i < m; i++) {  
 6             for (int j = 0; j < n; j++) {  
 7                 if (dfs(board, word, 0, i, j, visited))  
 8                     return true;  
 9             }  
         }  
         return false;  
     }
     
     public boolean dfs(char[][] board, String word, int index, int rowindex, int colindex, boolean[][] visited) {  
         if (index == word.length())  
             return true;  
         if (rowindex < 0 || colindex < 0 || rowindex >=board.length || colindex >= board[0].length)  
             return false;  
         if (visited[rowindex][colindex])  
             return false;  
         if (board[rowindex][colindex] != word.charAt(index))  
             return false;  
         visited[rowindex][colindex] = true;  
         boolean res = dfs(board, word, index + 1, rowindex - 1, colindex,  
                 visited)  
                 || dfs(board, word, index + 1, rowindex + 1, colindex, visited)  
                 || dfs(board, word, index + 1, rowindex, colindex + 1, visited)  
                 || dfs(board, word, index + 1, rowindex, colindex - 1, visited);  
         visited[rowindex][colindex] = false;  
         return res;  
             }

Reference:http://blog.csdn.net/yiding_he/article/details/18893621

Word Search leetcode java的更多相关文章

  1. Word Search [LeetCode]

    Problem Description: http://oj.leetcode.com/problems/word-search/ Basic idea: recursively go forward ...

  2. Word Ladder leetcode java

    题目: Given two words (start and end), and a dictionary, find the length of shortest transformation se ...

  3. Word Break leetcode java

    题目: Given a string s and a dictionary of words dict, determine if s can be segmented into a space-se ...

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

  5. LeetCode解题报告—— Word Search & Subsets II & Decode Ways

    1. Word Search Given a 2D board and a word, find if the word exists in the grid. The word can be con ...

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

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

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

随机推荐

  1. ZOJ 2702 Unrhymable Rhymes 贪心

    贪心.能凑成一组就算一组 Unrhymable Rhymes Time Limit: 10 Seconds      Memory Limit: 32768 KB      Special Judge ...

  2. STM32 TIMER OUTPUT DIAGRAM

  3. LPC-LINK 2

    LPC-Link 2 is an extensible, stand-alone debug adapter that can be configured to support various dev ...

  4. 在windows server 2008 R2 64bit上面配置PI OPC Server的DCOM

    今天想配置PI OPC SERVER的DCOM设置,但是发现在“运行dcomcnfg->组件服务-计算机-我的电脑-DCOM设置”中找不到PI OSI DA Server.如下图所示 这是以前从 ...

  5. JS实现各种复制到剪贴板

    一.实现点击按钮,复制文本框中的的内容                         <script type="text/javascript"> function ...

  6. 在ASP.NET MVC中通过勾选checkbox来更改select的内容

    遇到了这样的一个需求:通过勾选checkbox来更改select的内容. 在没有勾选checkbox之前是这样的: 在勾选checkbox之后是这样的: 想通过ajax异步来实现.所以,从控制器拿到的 ...

  7. CSS background-position用法

    相信很多喜欢研究网页界面的童鞋都遇到过一个奇妙的现象:网页中很多图片素材被合成在一张图片上. 起初小菜模仿网站的时候,经常遇到这个现象,那时候也不知道这时什么技术,人家的整张图片素材不会利用,只能用p ...

  8. IP地址和子网划分学习笔记之《预备知识:进制计数》

    一.序:IP地址和子网划分学习笔记开篇 只要记住你的名字,不管你在世界的哪个地方,我一定会去见你.——新海诚 电影<你的名字> 在我们的日常生活中,每个人的名字对应一个唯一的身(敏)份(感 ...

  9. 解决Oracle EM 乱码问题

    原创 作者:fa042 时间:2012-11-17 16:50:34 199 0 Oracle 10g提供了一个基于Web的管理工具EM(Enterprise Manager),使用比较方便.不过,如 ...

  10. java swing MenuItem乱码处理

    用java开发一个带有托盘图标的程序, 其它模块的中文显示都是正常的,就只有托盘中点击小图标时弹出的菜单中的中文是方框(中文方块) 解决: 1: 在你的具有main函数的类也即你应用运行的主类上点击右 ...