【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 ...
随机推荐
- java应用死循环排查方法或查找程序消耗资源的线程方法(面试)
今天遇到一个面试,怎么在一堆线程中查找一个死循环? 如果遇到线上应用cpu飙升,并出现OutOfMemery怎么办? 首先线上应用的jvm配置要养成良好的习惯,增加一下配置则可以在jvm发生 oom的 ...
- 在数学建模中学MATLAB
为期三周的数学建模国赛培训昨天正式结束了,还是有一定的收获的,尤其是在MATLAB的使用上. 1. 一些MATLAB的基础性东西: 元胞数组的使用:http://blog.csdn.net/z1137 ...
- solrCloud 管理
创建collection: /soft/server/solr-4.10.0/example/scripts/cloud-scripts/zkcli.sh -cmd upconfig -zkhost ...
- 转Web安全测试之XSS
转http://www.cnblogs.com/TankXiao/archive/2012/03/21/2337194.html XSS 全称(Cross Site Scripting) 跨站脚本攻击 ...
- LC.exe已退出,代码为-1错误
因为证书的原因,把项目中“properties”目录下的“license.licx”文件删除,再编译就成功了.如图:
- python sort和sorted的区别以及使用方法
iteralbe指的是能够一次返回它的一个成员的对象.iterable主要包括3类: 第一类是所有的序列类型,比如list(列表).str(字符串).tuple(元组). 第二类是一些非序列类型,比如 ...
- 这是一个比较清晰的SSH框架结构,有兴趣的博友可以看一下
http://www.cnblogs.com/zhaowancheng/p/5841591.html
- 解决C# 转到定义时打开的是元数据文件而非源代码文件的问题
原因:添加引用时 使用的是“浏览"选项卡,选择了项目生成的dll作为引用的内容. 解决:添加引用时 使用的是"项目"选项卡,选择了项目本身作为引用的内容.
- (转)MVC,MVP 和 MVVM 的图示
作者: 阮一峰 日期: 2015年2月 1日 复杂的软件必须有清晰合理的架构,否则无法开发和维护. MVC(Model-View-Controller)是最常见的软件架构之一,业界有着广泛应用.它本身 ...
- Linux档案与目彔的基本操作(查看与权限)
此文包含的命令: cd.pwd.mkdir.rmdir.rm.ls.cp.mv.cat.tac.more.less.head.tail.od.touch.umask.chattr.lsattr.fil ...