题目

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. 5969 [AK]刻录光盘

    题目描述 Description • 在FJOI2010夏令营快要结束的时候,很多营员提出来要把整个夏令营期间的资料刻录成一张光盘给大家,以便大家回去后继续学习.组委会觉得这个主意不错!可是组委会一时 ...

  2. MultiByteToWideChar和WideCharToMultiByte

    CString UTF8ToGB2312(CString str) { int len; // UTF8转换成Unicode len = MultiByteToWideChar(CP_UTF8, 0, ...

  3. Dell PowerEdge R710服务器内存条插法/Dell 11G/12G系列服务器内存条插法(转)

    说明:以我的经验,其实插3/6/9这个顺序去一定没有错. DELL PowerEdge R710服务器支持 DDR3的 DIMM (RDIMM) 或 ECC非缓冲的 DIMM(UDIMM).单列和双列 ...

  4. redis 写磁盘出错 Can’t save in background: fork: Cannot allocate memory (转)

    查看 Redis 日志 发现系统在频繁报错: [26641] 18 Dec 04:02:14 * 1 changes in 900 seconds. Saving… [26641] 18 Dec 04 ...

  5. Booting LPC-Link2, Updating LPCXpresso firmware

    Booting LPC-Link2 The recommended way to use LPC-Link2 with the LPCXpresso IDE is to boot and soft l ...

  6. How to convert a byte to its binary string representation

    How to convert a byte to its binary string representation For example, the bits in a byte B are 1000 ...

  7. code.google.com/p/log4go 下载失败

    用 glide 下载 goim 的依赖包时报错,提示: code.google.com/p/log4go 找不到,即下载失败 主要是 code.google.com 网站已关闭导致的, 有人把它 fo ...

  8. 自定义两行可左右滑动的GridView

    效果图: xml: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:an ...

  9. How to update WPF browser application manifest and xbap file with ‘mage.exe’

    老外参考文章1 老外参考文章2 I created a WPF browser application MyApp then published it by ClickOnce in VS2008. ...

  10. 解决Python交叉编译后,键盘方向键乱码的问题

    参考 http://www.alliedjeep.com/38071.htm https://www.zhihu.com/question/21518507 http://professor.blog ...