题目

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. leetcode 每个结点的右指针 python

    每个节点的右向指针     给定一个二叉树 struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *n ...

  2. 【WIN10】WIN2D——基本圖形的繪製

    DEMO下載地址:http://yunpan.cn/c3iNuHFFAcr8h (提取码:8e48) 先看一個截圖: 繪製了一些基本形狀. DEMO的繪製代碼都非常簡單,不想在博客裡細說了,看代碼更為 ...

  3. java集合系列之LinkList

    概要  第1部分 LinkedList介绍第2部分 LinkedList数据结构第3部分 LinkedList源码解析(基于JDK1.6.0_45) 第5部分 LinkedList示例 转载请注明出处 ...

  4. Stf-windows版本

    Stf-windows Stf 原项目地址:https://github.com/openstf/stf . 介绍 用于Web端设备远程管理 系统支持 支持Android版本2.3.3 (SDK10) ...

  5. 51Nod 1092 回文字符串(LCS + dp)

    51Nod 1092 数据结构暑假作业上出现的一题,学习了一下相关算法之后,找到了oj测试能AC. 1.回文串是一种中心对称的结构,这道题可以转变为求最长回文子序列长度的题目.(子序列:可以不连续) ...

  6. centos7安装rvm

    导入钥匙$ gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 若是提示 ...

  7. Sed&awk笔记之awk篇(转)

    Awk是什么 Awk.sed与grep,俗称Linux下的三剑客,它们之间有很多相似点,但是同样也各有各的特色,相似的地方是它们都可以匹配文本,其中sed和awk还可以用于文本编辑,而grep则不具备 ...

  8. HTML解析利器HtmlAgilityPack

    一个.NET下的HTML解析类库HtmlAgilityPack.HtmlAgilityPack是一个支持用XPath来解析HTML的类库,在花了一点时间学习了解HtmlAgilityPack的API和 ...

  9. Either, neither, both

    http://speakspeak.com/resources/english-grammar-rules/various-grammar-rules/either-neither-both One ...

  10. .Net Discovery 系列之二--string从入门到精通(下)

    前两节我们介绍了string的两个基本特性,如果你觉得你已经比较全面的了解了string,那么就来看看这第3.4两节吧. 三.有趣的比较操作  在第一节与第二节中,我们分别介绍了字符串的恒定性与与驻留 ...