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 ...
随机推荐
- 打开APK里的AndroidManifest.xml乱码
直接解压apk,打开AndroidManifest.xml显示乱码,因为这里面是二进制字符,和打开文件的编辑器无关.(也可以用ultraedit打开查看,有明文显示.只是看起来搜起来不是很方便而已) ...
- 剪短的python数据结构和算法的书《Data Structures and Algorithms Using Python》
按书上练习完,就可以知道日常的用处啦 #!/usr/bin/env python # -*- coding: utf-8 -*- # learn <<Problem Solving wit ...
- UVALive5031 Graph and Queries(Treap)
反向操作,先求出最终状态,再反向操作. 然后就是Treap 的合并,求第K大值. #include<cstdio> #include<iostream> #include< ...
- 8-06循环结构WHILE
WHILE 循环语句可以根据某些条件重复执行一条SQL语句或一个语句块. 语句: WHILE(条件) BEGIN 语句或语句块 END 程序调试: ALT+F5启动调试 F9切换断点 F10遂过程, ...
- Android 笔记 day1
- Delphi中滚动文字的应用
1.添加一个Timer控件,Interval属性设置为20. 2.添加一个Label控件,Name为labMessage. 3.在Timer的OnTimer事件添加如下代码: procedure TF ...
- Python入门练习
0.基本知识 Number.String.Lists 1.if判断的使用
- MySQL 存储过程控制语句
变量作用域内部的变量在其作用域范围内享有更高的优先权,当执行到end.变量时,内部变量消失,此时已经在其作用域外,变量不再可见了,应为在存储过程外再也不能找到这个申明的变量,但是你可以通过out参数或 ...
- 自己写了一个无缝滚动的插件(jQuery)
效果图: html代码: 1 <h1>无缝滚动,向右滚动</h1> 2 <ul id="guoul1"> 3 <li><img ...
- 2016最新 wamp2.5+windows 10安装CoedSgniffer代码格式检查:5分钟安装 30分钟入门和浏览常用命令
14:59 2016/1/112016最新 wamp2.5+windows 10安装CoedSgniffer代码格式检查:注意问题:1.手动安装2.5.0和pear安装方式都成功但是执行时无任何反映, ...