标签:style blog http color java 使用 os strong
题目:
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
,"SEE"
, -> returns true
,
word = "ABCB"
, -> returns false
.
题解:
这道题分析看,就是一个词,在一行出现也是true,一列出现也是true,一行往下拐弯也是true,一行往上拐弯也是true,一列往左拐弯也是true,一列往右拐弯也是true。所以是要考虑到所有可能性,基本思路是使用DFS来对一个起点字母上下左右搜索,看是不是含有给定的Word。还要维护一个visited数组,表示从当前这个元素是否已经被访问过了,过了这一轮visited要回false,因为对于下一个元素,当前这个元素也应该是可以被访问的。
代码如下:
Reference:http://blog.csdn.net/yiding_he/article/details/18893621
Word Search leetcode java,布布扣,bubuko.com
标签:style blog http color java 使用 os strong
原文地址:http://www.cnblogs.com/springfor/p/3883942.html