[抄题]:

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.

Example:

board =
[
['A','B','C','E'],
['S','F','C','S'],
['A','D','E','E']
] Given word = "ABCCED", return true.
Given word = "SEE", return true.
Given word = "ABCB", return false.

[暴力解法]:

时间分析:

空间分析:

[优化后]:

时间分析:

空间分析:

[奇葩输出条件]:

[奇葩corner case]:

[思维问题]:

一般的dfs都是主函数中调用一次就行了。搜索单词时,每个点都要调,因为每个点都有可能成为起点。

[一句话思路]:

单词的回溯法是dfs的一种,模式是:访问过-扩展-没访问过

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

[二刷]:

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

[总结]:

单词的回溯法是dfs的一种,模式是:访问过-扩展-没访问过

[复杂度]:Time complexity: O(4^n) Space complexity: O(n^2)

[英文数据结构或算法,为什么不用别的数据结构或算法]:

每个点都有可能开始:

//for loop
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board[0].length; j++) {
if (word.charAt(0) == board[i][j] && backtrace(board, i, j, word, 0)) return true;
}
}

boolean == true不用写,就是默认== true

[关键模板化代码]:

[其他解法]:

[Follow Up]:

[LC给出的题目变变变]:

2: trie树

[代码风格] :

class Solution {
static boolean[][] visited;
public boolean exist(char[][] board, String word) {
//ini:visited
visited = new boolean[board.length][board[0].length]; //for loop
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board[0].length; j++) {
if (word.charAt(0) == board[i][j] && backtrace(board, i, j, word, 0)) return true;
}
} return false;
} public boolean backtrace(char[][] board, int i, int j, String word, int index) {
//length
if (index == word.length()) return true; //exit:range, not equal, visited
if (i < 0 || i >= board.length || j < 0 || j >= board[0].length ||
word.charAt(index) != board[i][j] || visited[i][j]) return false; //expand
visited[i][j] = true;
if (backtrace(board, i + 1, j, word, index + 1) ||
backtrace(board, i - 1, j, word, index + 1) ||
backtrace(board, i, j + 1, word, index + 1) ||
backtrace(board, i, j - 1, word, index + 1)) return true;
visited[i][j] = false; return false;
}
}

79. Word Search在字母矩阵中查找单词的更多相关文章

  1. 79. 212. Word Search *HARD* -- 字符矩阵中查找单词

    79. Word Search Given a 2D board and a word, find if the word exists in the grid. The word can be co ...

  2. 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(单词查找)

    题目链接:https://leetcode.com/problems/word-search/#/description 给出一个二维字符表,并给出一个String类型的单词,查找该单词是否出现在该二 ...

  4. 刷题79. Word Search

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

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

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

  6. [LeetCode] Add and Search Word - Data structure design 添加和查找单词-数据结构设计

    Design a data structure that supports the following two operations: void addWord(word) bool search(w ...

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

  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 、212. Word Search II

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

随机推荐

  1. HTML:target=_blank

    _blank -- 在新窗口中打开链接 _parent -- 在父窗体中打开链接 _self -- 在当前窗体打开链接,此为默认值 _top -- 在当前窗体打开链接,并替换当前的整个窗体(框架页) ...

  2. C语言 数组排序法总结

    //快速排序法 void QuickSort(int *a, int left, int right) { int i = left; int j = right; int k = a[left]; ...

  3. elasticsearch 6.0.0及之后移除了一个索引允许映射多个类型的操作(Removal of mapping types)

    分给线一下内容为理解错误内容,实际允许建立父子分档,只是类型改成来 join 官方demo: join datatypeedit The join datatype is a special fiel ...

  4. 18.9.10 LeetCode刷题笔记

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

  5. 模拟Linux修改实际、有效和保存设置标识

    就是模拟setuid seteuid setreuid setresuid,感觉代码比书上大段的文字好记,就写成代码形式了. // setuid.cc: 模拟<unistd.h>中的设置用 ...

  6. JDBC--数据库链接及相关方法的封装

    使用的是MySQL数据库,首先导入驱动类,然后根据数据库URL和用户名密码获得数据的链接.由于使用的是MySQL数据库,它的URL一般为,jdbc:mysql://主机地址:端口号/库名. 下面是封装 ...

  7. 瞎记录java (含this 的用法)

    为类加一个方法: vid 方法名() 或者向下面这样写 给类加一个函数: 引用类里面的方法 引用不同类的方法: 其中 extends 应该是 本类可以去调用其他类里面的方法 的意思

  8. 【转】Jmeter使用之常用函数介绍

    "_csvRead"函数 csvRead函数是从外部读取参数,csvRead函数可以从一个文件中读取多个参数. 下面具体讲一下如何使用csvread函数: 1.新建一个csv或者t ...

  9. 我为什么一直不愿意用bootstrap

    做前端有2年多的时间了,知道bootstrap已经很久了. 第一次了解bootstrap是1年前,公司的一次培训中. 当时感到非常的愤怒,因为对框架的了解不够深入产生了这样的一个想法: 怎么会有这种框 ...

  10. sql sever数据库常用的执行语句

    --使用master数据库use master --创建数据库文件create database 数据库名字 on( name=, --逻辑名称 filename= .ndf, --数据文件物理路径名 ...