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.

解题思路:

开一个数组用于保存数据是否被访问过,使用DFS即可,JAVA实现如下:

static public boolean exist(char[][] board, String word) {
if (board.length == 0 || board[0].length == 0 || word.length() == 0)
return false;
boolean[][] isUsed=new boolean[board.length][board[0].length];
for (int i = 0; i < board.length; i++)
for (int j = 0; j < board[0].length; j++)
if (board[i][j] == word.charAt(0)){
isUsed[i][j]=true;
if(dfs(board,word,i,j,0,isUsed))
return true;
isUsed[i][j]=false;
}
return false;
}
public static boolean dfs(char[][] board,String word,int i,int j,int depth,boolean[][] isUsed){
if(depth==word.length()-1)
return true;
if(i>=1&&board[i-1][j]==word.charAt(depth+1)&&!isUsed[i-1][j]){
isUsed[i-1][j]=true;
if(dfs(board,word,i-1,j,depth+1,isUsed))
return true;
isUsed[i-1][j]=false;
}
if(i<=board.length-2&&board[i+1][j]==word.charAt(depth+1)&&!isUsed[i+1][j]){
isUsed[i+1][j]=true;
if(dfs(board,word,i+1,j,depth+1,isUsed))
return true;
isUsed[i+1][j]=false;
}
if(j>=1&&board[i][j-1]==word.charAt(depth+1)&&!isUsed[i][j-1]&&!isUsed[i][j-1]){
isUsed[i][j-1]=true;
if(dfs(board,word,i,j-1,depth+1,isUsed))
return true;
isUsed[i][j-1]=false;
}
if(j<=board[0].length-2&&board[i][j+1]==word.charAt(depth+1)&&!isUsed[i][j+1]){
isUsed[i][j+1]=true;
if(dfs(board,word,i,j+1,depth+1,isUsed))
return true;
isUsed[i][j+1]=false;
}
return false;
}

Java for LeetCode 079 Word Search的更多相关文章

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

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

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

  4. Java for LeetCode 126 Word Ladder II 【HARD】

    Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from ...

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

  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 79. Word Search 、212. Word Search II

    https://www.cnblogs.com/grandyang/p/4332313.html 在一个矩阵中能不能找到string的一条路径 这个题使用的是dfs.但这个题与number of is ...

  8. 【leetcode】Word Search

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

  9. [LeetCode]题解(python):079 Word Search

    题目来源 https://leetcode.com/problems/word-search/ Given a 2D board and a word, find if the word exists ...

随机推荐

  1. 【CodeForces 599A】D - 特别水的题4- Patrick and Shopping

    Description  meter long road between his house and the first shop and a d2 meter long road between h ...

  2. 【HDU 5578】Friendship of Frog

    题 题意 求相同字母最近距离 分析 用数组保存各个字母最后出现的位置,维护最小距离. 代码 #include <cstdio> int c[30],n,p,a,minl; char ch; ...

  3. tuple内部方法

    代码: #tuple内部方法 ac=('a','r','6','d','a','b','b','e') print(dir(ac)) print(ac.count('a')) print(ac.ind ...

  4. OAuth2.0认证和授权原理

    什么是OAuth授权?   一.什么是OAuth协议 OAuth(开放授权)是一个开放标准. 允许第三方网站在用户授权的前提下访问在用户在服务商那里存储的各种信息. 而这种授权无需将用户提供用户名和密 ...

  5. Eclipse在线安装ADT插件

    要想使用Eclipse开发Android应用,首先要安装一个ADT插件,在此记录一下在Eclipse中采用在线安装的方式ADT插件,我使用的Eclipse版本是:eclipse-jee-luna-SR ...

  6. jquery------.cycle的使用

    代码下载地址:http://malsup.github.io/jquery.cycle.all.js 把里面的代码复制到jquery.cycle.all.js里面 index.jsp <scri ...

  7. 初学Struts2-自定义拦截器及其配置

    自定义拦截器,首先新建一个继承自AbstractInterceptor类的类,然后重写intercept方法,代码如下 public class HelloInterceptor extends Ab ...

  8. JDK安装成功了,环境变量也配置好了,测试代码也可以运行,但是打不开eclipse

    解决办法:删除eclipse,重新解压后,将JDK文件夹下的jre文件夹拷贝到eclipse文件夹下,OK

  9. 远程桌面连接不上|windows server 2003 sp2 termdd.sys(转载)

    远程桌面连接不上|windows server 2003 sp2 termdd.sys.请教一个问题,为什么 Windows Server 2003 打上SP2补丁,就不能通过远程桌面连接上去了?服务 ...

  10. Linq 中 表连接查询

    public void Test(){ var query = from a in A join b in B on A.Id equals B.Id into c from d in c.Defau ...