【Leetcode】【Medium】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 =
[
["ABCE"],
["SFCS"],
["ADEE"]
]
word = "ABCCED", -> returns true,
word = "SEE", -> returns true,
word = "ABCB", -> returns false.
解题思路:
使用深度优先搜索的方法,从二维数组中的每个点出发,向上下左右进行延伸,根据判定条件及时判断;
解题步骤:
1、写一个主函数:
(1)获得二维数组的行列大小;
(2)遍历所有二维数组元素,对每个元素作为搜索起点,调用递归函数;
2、写一个递归函数,参数为:二维数组(引用)、待匹配的字符串(c字符串形式方便)、当前运动到的坐标值x和y、二维数组大小m和n;
(1)终止判定条件:x和y无效、当前运动到的字符不是字符串下一个字符;
(2)如果已经到达匹配字符串末尾,返回true
(3)暂时将当前运动到的坐标字符置换为'\0',因为要做出标记,以免重复折返运动;
(4)调用4个递归函数,分别运动到当前运动字符的上下左右四周,并判定;
(5)如果4个递归函数都false,则换回被置换的'\0',返回false;
代码:
class Solution {
public:
bool exist(vector<vector<char> > &board, string word) {
m = board.size();
n = board[].size();
for(int x = ; x < m; x++) {
for(int y = ; y < n; y++) {
if(isFound(board, word.c_str(), x, y))
return true;
}
}
return false;
}
private:
int m;
int n;
bool isFound(vector<vector<char> > &board, const char* w, int x, int y) {
if(x < || y < || x >= m || y >= n || *w != board[x][y])
return false;
if(*(w + ) == '\0')
return true;
char t = board[x][y];
board[x][y] = '\0';
if(isFound(board, w + , x - , y) ||
isFound(board, w + , x + , y) ||
isFound(board, w + , x, y - ) ||
isFound(board, w + , x, y + ))
return true;
board[x][y]=t;
return false;
}
};
【Leetcode】【Medium】word search的更多相关文章
- 【LeetCode题意分析&解答】35. Search Insert Position
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
- 【LeetCode题意分析&解答】40. Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- 【LeetCode题意分析&解答】37. Sudoku Solver
Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...
- ACM金牌选手整理的【LeetCode刷题顺序】
算法和数据结构知识点图 首先,了解算法和数据结构有哪些知识点,在后面的学习中有 大局观,对学习和刷题十分有帮助. 下面是我花了一天时间花的算法和数据结构的知识结构,大家可以看看. 后面是为大家 精心挑 ...
- 【leetcode刷题笔记】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每天一题】Word Search(搜索单词)
Given a 2D board and a word, find if the word exists in the grid.The word can be constructed from le ...
- 【leetcode刷题笔记】Word Ladder II
Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from ...
- 【LeetCode算法-58/66】Length of Last Word/Plus One
LeetCode第58题: Given a string s consists of upper/lower-case alphabets and empty space characters ' ' ...
- 【LeetCode题意分析&解答】33. 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 ...
- 【LeetCode每天一题】Length of Last Word(字符串中最后一个单词的长度)
Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...
随机推荐
- Python 前端之JS
JavaScript由浏览器编译运行 JS的导入方式有两种,一种直接定义,第二种通过src引入:可以存放在<head>头部,但是强烈建议放在<body>的最下面,因为如果你引入 ...
- easyui1.32 各种问题汇总
问题一 场景:tab切换,每个tab里用div放一个dataGrid,默认display:none隐藏,当display:'block'的时候,dataGrid会显示不全,仅显示一条线. 解决方法:切 ...
- RedHat下安装OPENCV
1.解压 unzip opencv-2.4.9.zip 2.进入目录,cmake CMakeLists.txt 生成build文件 3.使用命令 make 编译 4.使用命令 make instal ...
- 使用ssh-keygen设置ssh无密码登录
http://lhflinux.blog.51cto.com/1961662/526122 ssh-keygen -t rsa 输入后,会提示创建.ssh/id_rsa.id_rsa.pub的文件,其 ...
- Servlet 利用Session实现不重复登录
import java.io.IOException;import java.io.PrintWriter;import java.util.ArrayList;import java.util.It ...
- laravel 数据库迁移
问题:之前有创建迁移文件 并且执行过 如果删除迁移文件 再重新创建迁移文件时就有问题 提示找不到之前的迁移文件 /** 一开始执行的命令 php artisan make:migration crea ...
- 第1章 C#类型基础
1.1值类型和引用类型 1.1.1 值类型 使用值类型之前需要对值类型的所有元素初始化(普通值类型和结构体). 结构还有一个特性:调用结构上的方法前,需要对其所有的字段进行赋值,为了避免对结构体中所有 ...
- eval语句(sql)类似asp
执行字符串的SQL语句,相当于asp中的eval ***** declare @sql nvarchar(500) set @sql='select * from sk_hzp_fk_prescri ...
- 图解Android Studio导入Eclipse项目源码
方法/步骤 打开Android Studio,在主页面中选择"File"->"New"->"Import project...&quo ...
- JavaScript求最大数最小数
<!DOCTYPE html><html><head lang="en"> <meta charset="UTF-8" ...