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. Android Fresco (Facebook开源的图片加载管理库)

    Fresco是Facebook开源的一个图片加载和管理库. 这里是Fresco的GitHub网址. 同类型的开源库市面有非常多,比如Picasso, Universal Image Loader, G ...

  2. 【Aizu 2305】Beautiful Currency

    题 题意 给你n个货币价格,然后通过调整一些货币的大小,使得所有比自己小的货币都是该货币的约数,调整前第 i 货币为a,调整后为b 那么变化率为 ri=|a-b|/a ,总变化率为max(ri).求最 ...

  3. Jquery CDN

    新浪CDN <script src="http://lib.sinaapp.com/js/jquery/1.9.1/jquery-1.9.1.min.js"></ ...

  4. crossdomain.xml的配置详解

    目录 1 简介 2 crossdomain.xml的配置详解 3 总结 1 简介 flash在跨域时唯一的限制策略就是crossdomain.xml文件,该文件限制了flash是否可以跨域读写数据以及 ...

  5. html checkbox 全选与反选

    之所以记录这个博客,是因为我完全用jquery,无法实现自己想要的结果(通过一个checkbox的选中或不选中控制其他多个checkbox状态) <input type="checkb ...

  6. Javascript检测用户注册信息

    <html> <head> <title>用户注册</title> <meta http-equiv="content-type&quo ...

  7. 定义declare、%TYPE%、ROWTYPE、加循环

    %TYPE:定义一个变量,其数据类型与已经定义的某个 数据变量的类型相同,或者与数据库表的某个列的数据类型相同,这时可以使用%TYPE. %ROWTYPE PL/SQL 提供%ROWTYPE 操作符, ...

  8. 用开源Look&Feel (Substance)写 漂亮的Swing应用程序

    今天用Swing 做了一个模仿QQ2009的登录界面,用到了开源的Look&Feel (Substance),在使用的过程中遇到了一些问题,也学到了一些技巧.Substance (https: ...

  9. 关于thinkphp 开发的网站部署问题

    公司一个网站用thinkphp 开发的,由wamp环境移植到lamp环境 出现错误.提示无法生成缓存文件. 原因是thinkphp 的一些目录需要重新生成,所以将一个新的thinkphp 核心包应用后 ...

  10. tomcat服务器配置及使用

    序:tomcat作为免费开源的web服务器,广受大家喜欢,但是该如何使用此服务器呢?下面就一步一步教大家操作tomcat服务器 一.权限配置 编辑tomcat-users.xml文件配置tomcat服 ...