[抄题]:

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. 什么是YARN

    YARN的核心组件: 1)ResourceManager,扮演Master角色(和HDFS的nameNode很像)主要用于资源分配:RM有两个子组件,分别是Scheduler(Capacity Sch ...

  2. laravel的blade模板的布局嵌套

    测试路由 Route::get('/', function() { $value = [,,]; return view('home.index', array('data' => $value ...

  3. Juicer自定义函数

    首先,先写自定义的方法: function (sex) { ; ; var Range = Max - Min; var Rand = Math.random(); var res = (Min + ...

  4. 30个让人兴奋的视差滚动(Parallax Scrolling)效果网站--转

    视差滚动(Parallax Scrolling)是指让多层背景以不同的速度移动,形成立体的运动效果,带来非常出色的视觉体验.作为今年网页设计的热点趋势,越来越多的网站应用了这项技术.今天这篇文章就与大 ...

  5. 方法 中 void 的解释

  6. 028:基于mysqldump备份脚本

    MySQL Backup and Recovery 一 MySQL Backup 1.功能 mysqldump全量和增量备份,通过最近一次备份刷新产生binlog来定位执行增量. 脚本下载地址 git ...

  7. Mybatis内部原理与插件原理

    Mybatis的运行分为两大问题,第一部分是读取配置文件保存在Configuration对象中,用以创建SqlSessionFactory,第二部分是SqlSession的执行过程.相对而言SqlSe ...

  8. 浅谈PHP面向对象编程(六、自动加载及魔术方法)

    6.0 自动加载及魔术方法  6.1 自动加载 在PHP开发过程中,如果希望从外部引入一个class.通常会使用incluae和requre方法把定义这个class的文件包含进来.但是,在大型的开发项 ...

  9. ASP.NET MVC5入门指南

    1.创建项目 文件 --> 新建 --> 项目 Visual C# --> Web --> ASP.NET Web应用程序 MVC此时处于选中状态,勾选“添加单元测试”(可选择 ...

  10. Math对象及相关方法

    Math.abs() 取绝对值 Math.ceil()向上取整 (出现小数点就向上+1) Math.floor()向下取整 Math.round()四舍五入 Math.max(val1,val2,va ...