【LeetCode-面试算法经典-Java实现】【079-Word Search(单词搜索)】
【079-Word Search(单词搜索)】
【LeetCode-面试算法经典-Java实现】【全部题目文件夹索引】
原题
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.
题目大意
给定一个board字符矩阵,能够从随意一个点開始经过上下左右的方式走,每个点仅仅能走一次。假设存在一条路走过的字符等于给定的字符串。那么返回true
解题思路
以每个点作为起点。使用回溯法进行搜索
代码实现
算法实现类
public class Solution {
public boolean exist(char[][] board, String word) {
// 【注意我们假定输入的參数都是合法】
// 訪问标记矩阵,初始值默认会设置为false
boolean[][] visited = new boolean[board.length][board[0].length];
// 以每个位置为起点进行搜索,找到一个路径就停止
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board[0].length; j++) {
if (search(board, visited, i, j, word, new int[]{0})) {
return true;
}
}
}
return false;
}
/**
* @param board 字符矩阵
* @param visited 訪问标记矩阵
* @param row 訪问的行号
* @param col 訪问的列号
* @param word 匹配的字符串
* @param idx 匹配的位置,取数组是更新后的值能够被其他引用所见
* @return
*/
private boolean search(char[][] board, boolean[][] visited, int row, int col, String word, int[] idx) {
// 假设搜索的位置等于字串的长度,说明已经找到找到匹配的了
if (idx[0] == word.length()) {
return true;
}
boolean hasPath = false;
// 当前位置合法
if (check(board, visited, row, col, word, idx[0])) {
// 标记位置被訪问过
visited[row][col] = true;
idx[0]++;
// 对上,右,下,左四个方向进行搜索
hasPath = search(board, visited, row - 1, col, word, idx ) // 上
|| search(board, visited, row, col + 1, word, idx) // 右
|| search(board, visited, row + 1, col, word, idx) // 下
|| search(board, visited, row, col - 1, word, idx); // 左
// 假设没有找到路径就回溯
if (!hasPath) {
visited[row][col] = false;
idx[0]--;
}
}
return hasPath;
}
/**
* 判定訪问的位置是否合法
*
* @param board 字符矩阵
* @param visited 訪问标记矩阵
* @param row 訪问的行号
* @param col 訪问的列号
* @param word 匹配的字符串
* @param idx 匹配的位置
* @return
*/
public boolean check(char[][] board, boolean[][] visited, int row, int col, String word, int idx) {
return row >= 0 && row < board.length // 行号合法
&& col >= 0 && col < board[0].length // 列号合法
&& !visited[row][col] // 没有被訪问过
&& board[row][col] == word.charAt(idx); // 字符相等
}
}
评測结果
点击图片。鼠标不释放,拖动一段位置,释放后在新的窗体中查看完整图片。
特别说明
欢迎转载。转载请注明出处【http://blog.csdn.net/derrantcm/article/details/47248121】
【LeetCode-面试算法经典-Java实现】【079-Word Search(单词搜索)】的更多相关文章
- 079 Word Search 单词搜索
给定一个二维面板和一个单词,找出该单词是否存在于网格中.这个词可由顺序相邻的单元格内的字母构成,其中“相邻”单元格是那些水平相邻或垂直相邻的单元格.同一个单元格内的字母不允许被重复使用.例如,给定 二 ...
- [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]题解(python):079 Word Search
题目来源 https://leetcode.com/problems/word-search/ Given a 2D board and a word, find if the word exists ...
- LeetCode 79. Word Search单词搜索 (C++)
题目: Given a 2D board and a word, find if the word exists in the grid. The word can be constructed fr ...
- Leetcode79. Word Search单词搜索
给定一个二维网格和一个单词,找出该单词是否存在于网格中. 单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中"相邻"单元格是那些水平相邻或垂直相邻的单元格.同一个单元格内的字 ...
- 【LeetCode-面试算法经典-Java实现】【058-Length of Last Word (最后一个单词的长度)】
[058-Length of Last Word (最后一个单词的长度)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a string s consis ...
- 【LeetCode-面试算法经典-Java实现】【139-Word Break(单词拆分)】
[139-Word Break(单词拆分)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a string s and a dictionary of w ...
- 【LeetCode-面试算法经典-Java实现】【030-Substring with Concatenation of All Words(串联全部单词的子串)】
[030-Substring with Concatenation of All Words(串联全部单词的子串)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Yo ...
- 【LeetCode-面试算法经典-Java实现】【053-Maximum Subarray(最大子数组和)】
[053-Maximum Subarray(最大子数组和)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Find the contiguous subarray w ...
随机推荐
- WIN10配置instantclient
在PLSQL Developer目录下建立如下bat文件,替换其快捷方式,启动PLSQL Developer: @echo off set path=C:\instantclient-basic-nt ...
- luogu2051 [AHOI2009]中国象棋
巨水,调了好久,心态爆炸 #include <iostream> #include <cstring> #include <cstdio> using namesp ...
- 在 Yii2 项目中使用 Composer 添加 FontAwesome 字体资源
2014-06-21 19:05 原文 简体 繁體 2,123 次围观 前天帮同事改个十年前的网站 bug,页面上一堆 include require 不禁让人抱头痛哭.看到 V2EX 上的讨论说,写 ...
- BNUOJ 3580 Oulipo
Oulipo Time Limit: 1000ms Memory Limit: 65536KB This problem will be judged on PKU. Original ID: 3 ...
- 划分树C++版百度百科模板
此代码同hdu2665改编#include <iostream> #include <cstdio> #include<cstring> #include < ...
- 九度oj 题目1352:和为S的两个数字
题目描述: 输入一个递增排序的数组和一个数字S,在数组中查找两个数,是的他们的和正好是S,如果有多对数字的和等于S,输出两个数的乘积最小的. 输入: 每个测试案例包括两行: 第一行包含一个整数n和k, ...
- 九度oj 题目1030:毕业bg
题目描述: 每年毕业的季节都会有大量毕业生发起狂欢,好朋友们相约吃散伙饭,网络上称为“bg”.参加不同团体的bg会有不同的感觉,我们可以用一个非负整数为每个bg定义一个“快乐度”.现给定一个b ...
- DDLog-不同颜色打印信息
(一)下载安装 1.安装插件 XcodeColors Github 链接:https://github.com/robbiehanson/XcodeColors 打开XcodeColors项目,编译即 ...
- BZOJ 1009 [HNOI2008]GT考试 ——矩阵乘法 KMP
先用KMP处理所有的转移,或者直接暴力也可以. 然后矩阵快速幂即可. #include <cstdio> #include <cstring> #include <ios ...
- 多线程-java并发编程实战笔记
线程安全性 编写线程安全的代码实质上就是管理对状态的访问,而且通常都是共享的,可变的状态. 一个对象的状态就是他的数据,存储在状态变量中,比如实例域或静态域.所谓共享是指一个对象可以被多个线程访问:所 ...