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

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. 配置nodejs环境

    一.由于node有多种版本号,每个版本号的API肯定也有些细微的差别,在工作中有可能要经常切换node的版本号,因此可以下载nvm使其来管理node的版本号. 首先下载nvm,官网:https://g ...

  2. hive 普通创建表和跟新列操作

    创建表 CREATE TABLE if not exists student ( student_id int, sex int, address String, email String ) 这里需 ...

  3. Linux下的tmpfs文件系统(/dev/shm)

    转自:http://www.2cto.com/os/201411/354888.html 介绍 /dev/shm/是一个使用就是tmpfs文件系统的设备,其实就是一个特殊的文件系统.redhat中默认 ...

  4. [perl]字符串转拼音首字母(支持多音字)

    实现的思路是,查表找到该字的所有读音,然后取首字母. 代码: while (<DATA>) { chomp; })(.*)$/; $all =~ s/^\s+//; ### 只保留无音标号 ...

  5. Intent传递list<bean>集合

    首先你定义的bean需要继承Serializable接口 //传递list<bean>集合Intent intent = new Intent(ViolationOfTheQueryAct ...

  6. AutoCompleteTextView自动补全文本框

    AutoCompleteTextView的作用是在输入框中输入我们想要输入的信息,就会出现其他与其相关的提示信息 下面是实例代码: MainActivity.java package com.shao ...

  7. File类基础

    File类的作用: Java的io包中定义了File类,用于对文件或文件夹的管理操作. File类只能够用于表示文件或文件夹的信息(属性)和对该文件或文件夹的删除创建操作 (不能对内容进行访问) 通过 ...

  8. iOS--异步下载

    #import "ViewController.h"#import "UIImageView+WebCache.h"@interface ViewControl ...

  9. 坑爹坑娘坑祖宗的87端口(记一次tomcat故障排查)

    原贴如下 坑爹坑娘坑祖宗的87端口(记一次tomcat故障排查) 虽然我用的是PHPstudy部署的dedecms,还是一样栽倒这个坑里了. 总结经验:本地测试使用8000~9000的端口比较安全.

  10. iOS 为label加删除线

    NSString *oldPrice = [NSString stringWithFormat:@"原价 %@",_item.previousPrice.stringValue]; ...