使用的别人的思路,用一个二维数组记录每一个位置是否用过,然后通过递归来判断每一个位置是否符合

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的更多相关文章

  1. 刷题79. Word Search

    一.题目说明 题目79. Word Search,给定一个由字符组成的矩阵,从矩阵中查找一个字符串是否存在.可以连续横.纵找.不能重复使用,难度是Medium. 二.我的解答 惭愧,我写了很久总是有问 ...

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

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

  5. leetcode 79. Word Search 、212. Word Search II

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

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

  7. Leetcode#79 Word Search

    原题地址 依次枚举起始点,DFS+回溯 代码: bool dfs(vector<vector<char> > &board, int r, int c, string ...

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

  9. 【一天一道LeetCode】#79. Word Search

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

随机推荐

  1. 【python】点分十进制ip与数字互转

    来源:http://www.cnblogs.com/vovlie/archive/2012/10/17/2727029.html 利用lambda表达式,非常简洁 int_to_ip = lambda ...

  2. iOS之ToolBar定制

    ToorBar的定制 在诸如社区类的app里面,很多都涉及到用户发布消息,如现今最流行的新浪微博,每条信息底部都会有个工具条,正如下图所示,有转发.评论和点赞三个按钮的工具条. 结构 1.作为一个独立 ...

  3. android 扫描

    http://blog.csdn.net/abidepan/article/details/11902041

  4. GitHub团队项目合作流程

    已在另一篇博客中写出关于以下问题的解决,点此进入: 同步团队项目到本地时出现冲突怎么办? 项目负责人merge一个Pull Request后发现有错怎么回退? 目录: 零.前期准备 一.创建开发分支 ...

  5. tp框架之查询

    //数据访问        //造模型对象        $nation = D("Nation");                //查询        //$a = $nat ...

  6. 最小化安装centos7不能执行ifconfig命令的解决方法

    操作环境 虚拟机:VirtualBox 5.0.4 for Windows 操作系统:CentOS-7-x86_64-Minimal-1503-01.iso 问题描述 登录系统之后,输入ifconfi ...

  7. JS高级程序设计笔记一

    /*设置自定义属性*/ var div=document.querySelector("#div1"); div.setAttribute("title",&q ...

  8. oj Rapid Typing

    import bs4 import requests import urllib2 import time import base64 session=requests.Session() respo ...

  9. NoSQL-Redis【2】-HDEL给我的一个惊喜

    用HDEL删除Hash最后一个域的时候,Redis奇迹般的帮我清除了Key,真是考虑周到啊! 127.0.0.1:6673[2]> HMSET USER:Zhangshan Name 'Zhan ...

  10. canvas画布属性globalAlpha 和 createRadialGradient函数出现的设置问题

    今天用canvas做了一个页面特效,呼呼,在做的过程中发现createRadialGradient 和 globalAlpha这2个属性一起使用导入不能实现透明度问题,首先把createRadialG ...