【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 ...
随机推荐
- ajax XML
<script src="jquery-1.11.2.min.js"></script> </head> <body> <se ...
- Egret Wing3 FTP使用方法
FTP 挺实用的,不用自己去申请sinasea什么的免费空间来测试项目了. 添加FTP服务器配置 默认就行. 指定目录上传至FTP服务器 选择免费云测试空间.然后选择bin-release/web目录 ...
- oracle net manager的配置文件tnsnames.ora位置
配置文件所在的路径:C:\app\Ling-PC\product\11.2.0\client_1\network\admin (红色为安装的盘符位置)
- Log4Net日志的配置
<configuration> <configSections> <section name="log4net" type="log ...
- linq to entity中遇到的问题
当使用 from m in _db.students从数据库中获取数据时,数据库中的数据类型和C#中的不同,所以可能会出错!先作_db.students.ToList()然后select
- asp.net GDI+绘制多个矩形
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...
- afterTextChanged() callback being called without the text being actually changed
afterTextChanged() callback being called without the text being actually changed up vote8down votefa ...
- Groovy basic
1. print println "Hello Groovy!" you can use java in Groovy System.out.println("Hello ...
- 记一次 Ubuntu 使用 arptables 抵御局域网 ARP 攻击
. . . . . 前段时间大概有一个月左右,租房的网络每天都断一次,每次断大概一两分钟左右就恢复了,所以没太在意.直到有一天晚上,LZ 正在写博客,但是网络频繁中断又重新连上再中断.待 LZ 好不容 ...
- 5.HotSpot的算法实现
1.枚举根节点 在可达性分析中,可以作为GC Roots的节点有很多,但是现在很多应用仅仅方法区就有上百MB,如果逐个检查的话,效率就会变得不可接受. 而且,可达性分析必须在一个一致性的快照中进行-即 ...