Given a 2D board and a word, find if the word exists in the grid.

The word can be constructed from letters of sequentially adjacent cell, where "adjacent" cells are those horizontally or vertically neighboring. The same letter cell may not be used more than once.

For example,
Given board =

[
["ABCE"],
["SFCS"],
["ADEE"]
]

word = "ABCCED", -> returns true,
word = "SEE", -> returns true,
word = "ABCB", -> returns false.


题解:实现一个DFS函数 private boolean canFind(char[][] board,int i,int j,String left,boolean[][] visited) ,从(i,j)出开始搜索串left,visited数组记录某个位置是否包含在当前搜索的路径中,因为每个字符只能被使用一次。

代码如下:

 public class Solution {
private boolean canFind(char[][] board,int i,int j,String left,boolean[][] visited){
//if we have found one path
if(left.equals(""))
return true; //look around point(i,j) see if we can find next char
char next = left.charAt(0);
if(i-1>=0 && !visited[i-1][j] && board[i-1][j]== next ){
visited[i-1][j] = true;
if(canFind(board, i-1, j, left.substring(1),visited))
return true;
visited[i-1][j]= false;
} if(j-1>=0 && !visited[i][j-1] && board[i][j-1]== next ){
visited[i][j-1] = true;
if(canFind(board, i, j-1, left.substring(1),visited))
return true;
visited[i][j-1]= false;
} if(i+1 < board.length && !visited[i+1][j] && board[i+1][j]== next ){
visited[i+1][j] = true;
if(canFind(board, i+1, j, left.substring(1),visited))
return true;
visited[i+1][j]= false;
} if(j+1 < board[0].length && !visited[i][j+1] && board[i][j+1]== next ){
visited[i][j+1] = true;
if(canFind(board, i, j+1, left.substring(1),visited))
return true;
visited[i][j+1]= false;
} return false;
}
public boolean exist(char[][] board, String word) {
if(word == null || word.length() == 0)
return true;
if(board.length == 0)
return false; int m = board.length;
int n = board[0].length;
char now = word.charAt(0);
boolean[][] visited = new boolean[m][n]; //search board for our first char in word
for(int i = 0;i < m;i++){
for(int j = 0;j < n;j ++){
if(board[i][j]== now ){
visited[i][j]= true;
if(canFind(board, i, j, word.substring(1),visited))
return true;
visited[i][j]= false;
}
}
} return false;
}
}

【leetcode刷题笔记】Word Search的更多相关文章

  1. 【leetcode刷题笔记】Search in Rotated Sorted Array II

    Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this ...

  2. 【leetcode刷题笔记】Search in Rotated Sorted Array

    Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...

  3. 【leetcode刷题笔记】Search a 2D Matrix

    Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...

  4. LeetCode刷题笔记和想法(C++)

    主要用于记录在LeetCode刷题的过程中学习到的一些思想和自己的想法,希望通过leetcode提升自己的编程素养 :p 高效leetcode刷题小诀窍(这只是目前对我自己而言的小方法,之后会根据自己 ...

  5. 18.9.10 LeetCode刷题笔记

    本人算法还是比较菜的,因此大部分在刷基础题,高手勿喷 选择Python进行刷题,因为坑少,所以不太想用CPP: 1.买股票的最佳时期2 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格. ...

  6. LeetCode刷题笔记 - 12. 整数转罗马数字

    学好算法很重要,然后要学好算法,大量的练习是必不可少的,LeetCode是我经常去的一个刷题网站,上面的题目非常详细,各个标签的题目都有,可以整体练习,本公众号后续会带大家做一做上面的算法题. 官方链 ...

  7. 刷题79. Word Search

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

  8. Leetcode刷题笔记(双指针)

    1.何为双指针 双指针主要用来遍历数组,两个指针指向不同的元素,从而协同完成任务.我们也可以类比这个概念,推广到多个数组的多个指针. 若两个指针指向同一数组,遍历方向相同且不会相交,可以称之为滑动窗口 ...

  9. 【leetcode刷题笔记】Word Ladder II

    Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from ...

随机推荐

  1. JS 导出Table为excel的三种可行方法

    [html] view plain copy<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" &q ...

  2. 【Lucene】Apache Lucene全文检索引擎架构之中文分词和高亮显示4

    前面总结的都是使用Lucene的标准分词器,这是针对英文的,但是中文的话就不顶用了,因为中文的语汇与英文是不同的,所以一般我们开发的时候,有中文的话肯定要使用中文分词了,这一篇博文主要介绍一下如何使用 ...

  3. sklearn--feature extract--人脸识别

    1.原始数据加载 import matplotlib.pyplot as plt from sklearn.datasets import fetch_lfw_people people=fetch_ ...

  4. tony_update yum

    更改方法是这样的 在 /etc/yum.repos.d 下 1  wget http://mirrors.163.com/.help/CentOS6-Base-163.repo 2  #mv  Cen ...

  5. 使用 yarn 作为 Npm 的代替方案

    相关传送门: # window 安装包下载https://yarnpkg.com/zh-Hans/docs/install#windows-stable # yarn官方网站 https://yarn ...

  6. hdu 1811 Rank of Tetris(拓扑,并查集)

    题意:略 分析:排序先按rating,若相同,则按rp.考虑到每个人的rp均不同,所以rating相同的人必然可以排序.那么只需要考虑rating不同的集合了.  大小关系可以用有向边表示,而大小关系 ...

  7. Webpack与Gulp、Grunt区别

    Webpack与Gulp.Grunt没有什么可比性,它可以看作模块打包机,通过分析你的项目结构,找到JavaScript模块以及其它的一些浏览器不能直接运行的拓展语言(Scss,TypeScript等 ...

  8. [译]GLUT教程 - 交换菜单

    Lighthouse3d.com >> GLUT Tutorial >> Pop-up Menus >> Swapping Menus GLUT甚至可以在应用程序过 ...

  9. html自定义标签属性

    <a href="#" _asd="xxxx" onclick="test(event)">test</a> < ...

  10. 基于markdown的blog系统调研1:typecho

    ))