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 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 =
[
['A','B','C','E'],
['S','F','C','S'],
['A','D','E','E']
]
word = "ABCCED", -> returns true,
word = "SEE", -> returns true,
word = "ABCB", -> returns false.
题目标签:Array
Java Solution:
Runtime beats 34.54%
完成日期:07/27/2017
关键词:Array
关键点:Braktracking;新矩阵记录探索过的点;每个点有4个方向可以探索
public class Solution
{
public boolean exist(char[][] board, String word)
{
if(board == null || board.length == 0
|| board.length * board[0].length < word.length())
return false; boolean[][] mark = new boolean[board.length][board[0].length];
boolean res = false;
// iterate board, find the match starting character to pass to findWord function
for(int i=0; i<board.length; i++)
{
for(int j=0; j<board[0].length; j++)
{
if(board[i][j] == word.charAt(0))
res = res || findWord(board, word, 0, i, j, mark); if(res)
return res;
}
} return res;
} public boolean findWord(char[][] board, String word, int wordIndex,
int row, int col, boolean[][] markBoard)
{
// base case 1: if exceed word's length, meaning it is done and found the word
if(wordIndex == word.length())
return true; /* base case 2: if this character is out of bound or
* this character is not match to word's character or
* hits character has been already visited
*/
if(row >= board.length || row < 0 || col >= board[0].length || col < 0
|| word.charAt(wordIndex) != board[row][col] || markBoard[row][col])
return false; // mark this char as visited
markBoard[row][col] = true; // follow top, right, bottom, left order to check character
// if any direction future path return true, meaning no need to continue other directions
if(findWord(board, word, wordIndex + 1, row - 1, col, markBoard) || // go top
findWord(board, word, wordIndex + 1, row, col + 1, markBoard) || // go right
findWord(board, word, wordIndex + 1, row + 1, col, markBoard) || // go bottom:
findWord(board, word, wordIndex + 1, row, col - 1, markBoard)) // go left:
{
return true;
} markBoard[row][col] = false; // clear the mark of this character // if this this character's all four directions path has failed, return false to last level
return false;
}
}
参考资料:N/A
LeetCode 算法题目列表 - LeetCode Algorithms Questions List
LeetCode 79. 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 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 ...
- [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 79 Word Search(单词查找)
题目链接:https://leetcode.com/problems/word-search/#/description 给出一个二维字符表,并给出一个String类型的单词,查找该单词是否出现在该二 ...
- [LeetCode OJ] Word Search 深度优先搜索DFS
Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from l ...
- 079 Word Search 单词搜索
给定一个二维面板和一个单词,找出该单词是否存在于网格中.这个词可由顺序相邻的单元格内的字母构成,其中“相邻”单元格是那些水平相邻或垂直相邻的单元格.同一个单元格内的字母不允许被重复使用.例如,给定 二 ...
- Leetcode79. Word Search单词搜索
给定一个二维网格和一个单词,找出该单词是否存在于网格中. 单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中"相邻"单元格是那些水平相邻或垂直相邻的单元格.同一个单元格内的字 ...
- Leetcode#79 Word Search
原题地址 依次枚举起始点,DFS+回溯 代码: bool dfs(vector<vector<char> > &board, int r, int c, string ...
随机推荐
- python import xxx 与 from xxx import xx 模块引入的区别
有如下脚本script1.py: A='aaaa'B='bbbb'C='cccc'print A,B,C 1.命令行交互模式下使用import 导入方式1: >>>import sc ...
- 初入ubuntu
登入root :su root 安装 vim: sudo apt-get install vim 安装 gcc(g++):sudo apt-get install gcc(g++) 非常实用的修改分辨 ...
- Python之面向对象与类
本节内容 面向对象的概念 类的封装 类的继承 类的多态 静态方法.类方法 和 属性方法 类的特殊成员方法 子类属性查找顺序 一.面向对象的概念 1. "面向对象(OOP)"是什么? ...
- 关于DbContext能不能单次请求内唯一?DbContex需不需要主动释放?欢迎各路大侠来“参战”!
基于前篇文章<HiBlogs>重写笔记[1]--从DbContext到依赖注入再到自动注入园友@Flaming丶淡蓝@ 吴瑞祥 提出了讨论和质疑,吓得我连夜查询资料(玩笑~). 本来文章的 ...
- java 读取properties文件总结
一.java读取properties文件总结 在java项目中,操作properties文件是经常要做的,因为很多的配置信息都会写在properties文件中,这里主要是总结使用getResource ...
- java GUI编程二
java基础学习总结--GUI编程(二) 一.事件监听 测试代码一: 1 package cn.javastudy.summary; 2 3 import java.awt.*; 4 import j ...
- MyCAT-EYE开源
MyCAT EYE MySQL数据库监控工具,实现了对MySQL节点的管理和监控,可供开发人员和DBA使用.后续版本将整合MyCAT2.0的管理和配置. 演示地址: 开发人员视图:http://120 ...
- Ubuntu 16安装GPU版本tensorflow
pre { direction: ltr; color: rgb(0, 0, 0) } pre.western { font-family: "Liberation Mono", ...
- .net窗体程序的基础知识及详细笔记
第一章:初识Windows程序 1.1:第一个wondows程序 1.1.1:认识windows程序 Form1.cs:窗体文件:程序对窗体编写的代码一般都存放在这个文件(还有拖动控件时的操作和布局, ...
- Struts201---环境搭配
开发工具:Eclipse Struts版本:2.3.24 最近在学SSH框架,SSH是 struts+spring+hibernate的一个集成框架,是目前比较流行的一种Web应用程序开源框架.集 ...