79. Word Search
使用的别人的思路,用一个二维数组记录每一个位置是否用过,然后通过递归来判断每一个位置是否符合
public class Solution {
public boolean exist(char[][] board, String word) {
if(board.length == 0) return false;
int leni = board.length;
int lenj = board[0].length;
boolean[][] isVisited = new boolean[leni][lenj];
for(int i=0;i < leni;++i){
for(int j = 0; j < lenj;++j){
isVisited[i][j]= false;
}
}
for(int i = 0; i < leni;++i){
for(int j = 0 ; j < lenj;++j){
if(board[i][j] == word.charAt(0))
{
isVisited[i][j] = true;
if(WordSearch(board,isVisited,word.substring(1),i,j)){
return true;
}
isVisited[i][j] = false;
}
}
}
return false;
}
public boolean WordSearch(char[][] board,boolean[][] isVisited,String word,int i, int j){
if(word.length() == 0) return true;
int[][] direction={{0,1},{0,-1},{-1,0},{1,0}};//上下左右
for(int k = 0; k < direction.length;++k){
int x = i + direction[k][0];
int y = j + direction[k][1];
if((x >= 0 && x < board.length) &&
(y >= 0 && y < board[i].length) &&
board[x][y] == word.charAt(0)&&
isVisited[x][y] == false){
isVisited[x][y] = true;
if(WordSearch(board,isVisited,word.substring(1), x, y)){
return true;
}
isVisited[x][y] = false;
}
}
return false;
}
}
79. Word Search的更多相关文章
- 刷题79. Word Search
一.题目说明 题目79. Word Search,给定一个由字符组成的矩阵,从矩阵中查找一个字符串是否存在.可以连续横.纵找.不能重复使用,难度是Medium. 二.我的解答 惭愧,我写了很久总是有问 ...
- [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
Word Search Given a 2D board and a word, find if the word exists in the grid. The word can be constr ...
- [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 OJ 79. Word Search
题目 Given a 2D board and a word, find if the word exists in the grid. The word can be constructed fro ...
- Leetcode#79 Word Search
原题地址 依次枚举起始点,DFS+回溯 代码: bool dfs(vector<vector<char> > &board, int r, int c, string ...
- 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
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
随机推荐
- 【XLL 框架库函数】 TempInt/TempInt12
创建一个包含了 integer 的 XLOPER/XLOPER12 原型 LPXLOPER TempInt(short int i); LPXLOPER12 TempInt12(int i); 参数 ...
- cocoaPods 的使用
打开 终端 1. 移除系统自带的 因为该rub已经被和谐了 使用ruby.taobao.org MCJ:~ MCJ$ sudo gem sources -l *** CURRENT SOURCES * ...
- java ArrayList 实现
关于ArrayList的实现和原理,原文出处:http://www.cnblogs.com/ITtangtang/p/3948555.html 我觉得他写的非常好,真的很好. 做一个记录和总结吧 pu ...
- 编译带有PROJ4和GEOS模块的GDAL
1.下载三个软件的源代码(去各自官网下载即可) 2.将PROJ4和GEOS的源码放到GDAL目录下的supportlibs文件夹中. 3.修改GDAL的nmake.opt文件,部分内容如下: # Un ...
- ORACLE用户创建&删除
●sqlplus登陆sqlplus sys/isc@testgmmc as sysdba●创建用户create user testpoi3 IDENTIFIED by iscaccount unloc ...
- Linux yum配置文件详解
说明:经过网上抄袭和自己的总结加实验,非常详细,可留作参考. yum的配置一般有两种方式: 一种是直接配置/etc目录下的yum.conf文件, 另外一种是在/etc/yum.repos.d目录下 ...
- 前端Js跨域方法汇总—剪不断,理还乱,是跨域
1.通过jsonp跨域2.通过修改document.domain来跨子域(iframe)3.隐藏的iframe+window.name跨域4.iframe+跨文档消息传递(XDM)5.跨域资源共享 C ...
- 【转】c# Image获得图片路径的三种方法 winform
代码如下:c# pictureBox1.Image的获得图片路径的三种方法 winform 1.绝对路径:this.pictureBox2.Image=Image.FromFile("D:\ ...
- Android之菜单总结
在Android中,菜单被分为如下三种,选项菜单(OptionsMenu).上下文菜单(ContextMenu)和子菜单(SubMenu). 1. 选项菜单(OptionsMenu)详解 Activi ...
- BestCoder 2nd Anniversary
A题 Oracle http://bestcoder.hdu.edu.cn/contests/contest_chineseproblem.php?cid=703&pid=1001 大数相加: ...