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 ...
随机推荐
- Yii 动作过滤的方法
protected function _init() { } public function beforeAction($action) { //黑名单 $blackList = array('tes ...
- Power BI for Office 365(六)Power Map简介
如果说Power BI中最给力的功能是什么,我觉得是Power Map.Power Map第一次是出现在SQL Server 2014的新特性里被提及,前身就是GeoFlow.在Power Map下可 ...
- Android 笔记 day3
短信发送器 电脑弱爆了,开第二个emulater要10min!!! 顺便学会查看API文档 package com.example.a11; import java.util.*; import an ...
- java打包文件夹为zip文件
//待压缩的文件目录 String sourceFile=sourceFilePath+"\\"+userName; //存放压缩文件的目录 String zipFilePath ...
- SwipeRefreshLayout下拉刷新简单用例
自己的下拉刷新组件 下拉刷新并自动添加数据 MainActivity package com.shaoxin.myswiperefreshlayout; import android.graphics ...
- 请问MVC4是不是类似于html页+ashx页之间用JSON通过AJAX交换数据这种方式、?
不是,可以讲mvc模式是借鉴于java下面的mvc开发模式,为开发者公开了更多的内容和控制,更易于分工合作,与单元测试,借用官方的说法:MVC (Model.View.Controller)将一个We ...
- NOIP200805 笨小猴(低效算法)(一大桶水)【A006】
[A006]笨小猴[难度A]—————————————————————————————————————————————————————————————— [题目要求] 笨小猴的词汇量很小,所以每次做英 ...
- Html_color code表示
http://www.computerhope.com/htmcolor.htm#color-codes,如
- 怎样看懂Oracle的执行计划
怎样看懂Oracle的执行计划 一.什么是执行计划 An explain plan is a representation of the access path that is taken when ...
- Poj2479 & Poj 2593
就是按着DP的思路来做的,结果还是想不到.T_T,行了,别玻璃心了,继续. 这道题目是求在一列数里,由两部分子段和组成的最大和.即对于连续整数组成的串 S1.S2,使 S1 + S2 的和最大. 题目 ...