Java for LeetCode 079 Word Search
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的更多相关文章
- 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 ...
- [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 ...
- [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 ...
- Java for LeetCode 126 Word Ladder II 【HARD】
Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from ...
- [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 ...
- [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 ...
- leetcode 79. Word Search 、212. Word Search II
https://www.cnblogs.com/grandyang/p/4332313.html 在一个矩阵中能不能找到string的一条路径 这个题使用的是dfs.但这个题与number of is ...
- 【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 ...
- [LeetCode]题解(python):079 Word Search
题目来源 https://leetcode.com/problems/word-search/ Given a 2D board and a word, find if the word exists ...
随机推荐
- 从Yii2的Request看其CSRF防范策略
用ajax请求还是用命令行CURL请求总是会得到 http400:Bad Request的错误, 而如果用Web网页方式GET访问(去除verbFilter的POST限制),是正常的,是CSRF验证的 ...
- ssh整合常见的错误
1.报错信息:java.lang.IllegalStateException: BeanFactory not initialized or already closed - call 'refres ...
- Fedora和Ubuntu下安装OpenGL开发环境配置
Fedora下OpenGl开发环境配置 开发OpenGL工程需要3个库文件和对应的头文件: libglut.so,libGLU.so,libGL.so, gl.h ,glu.h, glut.h 这些库 ...
- BZOJ-3229 石子合并 GarsiaWachs算法
经典DP?稳T 3229: [Sdoi2008]石子合并 Time Limit: 3 Sec Memory Limit: 128 MB Submit: 426 Solved: 202 [Submit] ...
- mysql中的if判断
问题是这样的,有一张表(tb_class)专门保存班级的ID和班级的名字 另一张表是学生信息表(tb_stu),表中有一个字段叫classID,没有外键关联,现在要把 这张表刷新到另一个表tb_par ...
- codeforces 86D : Powerful array
Description An array of positive integers a1, a2, ..., an is given. Let us consider its arbitrary su ...
- Windows Management Instrumentation WMI Security Technology Learning
目录 . 引言 . WMI(Windows Management Instrumentation)简介 . 基于WMI的攻击向量 . WMI编程示例 0. 引言 在进行服务器主机的入侵检测.安全攻防的 ...
- Jquery CDN
新浪CDN <script src="http://lib.sinaapp.com/js/jquery/1.9.1/jquery-1.9.1.min.js"></ ...
- python库requests登录zhihu
废了很大劲,开始搞错了登录post信息的网址,后来没找到xsrf信息,看了很多文章才搞定. 大概过程如下: 打开登录页面,同时打开fldder,输入信息去监控过程. 查看post了哪些信息,哪些是自己 ...
- inux环境PHP7.0安装
inux环境PHP7.0安装 PHP7和HHVM比较PHP7的在真实场景的性能确实已经和HHVM相当, 在一些场景甚至超过了HHVM.HHVM的运维复杂, 是多线程模型, 这就代表着如果一个线程导 ...