当前位置:网站首页>LeetCode79题 方法一:深度搜索
LeetCode79题 方法一:深度搜索
2022-07-28 17:02:00 【l齐天】
- 单词搜索
给定一个二维网格和一个单词,找出该单词是否存在于网格中。
单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中“相邻”单元格是那些水平相邻或垂直相邻的单元格。同一个单元格内的字母不允许被重复使用。
示例:
board =
[
[‘A’,‘B’,‘C’,‘E’],
[‘S’,‘F’,‘C’,‘S’],
[‘A’,‘D’,‘E’,‘E’]
]
给定 word = “ABCCED”, 返回 true
给定 word = “SEE”, 返回 true
给定 word = “ABCB”, 返回 false
提示:
board 和 word 中只包含大写和小写英文字母。
1 <= board.length <= 200
1 <= board[i].length <= 200
1 <= word.length <= 10^3
执行用时: 184 ms
内存消耗: 120.8 MB
class Solution {
public:
bool check(vector<vector<char>>& board, vector<vector<int>>& visited, string word, int i, int j, int k, int h, int w) {
//i j是当前点的坐标 k是用于标记那个字符需要被查询
/* 两个逻辑 如果这个元素不是则回退 如果已经找到了则退出 */
if (board[i][j] != word[k]) {
return false;
}
else if (k == word.length() - 1) {
return true;
}
//设置visited
visited[i][j] = 1;
//找到了这个元素但是没有结束
k += 1;//找到了这个元素即可以自增
int temp_i;
int temp_j;
//设置偏移量
vector<pair<int, int>>direction{
{
0, 1},{
0 ,-1},{
1, 0},{
-1, 0} };//右 左 上 下
for (int n = 0; n < 4; n++) {
temp_i = i + direction[n].first;
temp_j = j + direction[n].second;
if (temp_i == h || temp_j == w || temp_i == -1 || temp_j == -1)continue;
if (!visited[temp_i][temp_j]) {
//cout << "(" << temp_i + 1 << "," << temp_j + 1 << ")" << endl;
if (check(board, visited, word, temp_i, temp_j, k, h, w))return true;
}
}
//经过四次循环发现找不到可以使用的元素 此时将其复原为0 这点很重要
visited[i][j] = 0;
return false;
}
bool exist(vector<vector<char>>& board, string word) {
int i, j, k;
int h = board.size(), w = board[0].size();
//获取开头第一个字母的位置 确定起始点
char star = word[0];//得到第一个字母
vector<int>coordinate;//储存坐标
//将坐标成功储存在coordinate中
for (i = 0; i < h; i++) {
for (j = 0; j < w; j++) {
if (board[i][j] == star) {
coordinate.push_back(i);
coordinate.push_back(j);
}
}
}
//初始化一个检查矩阵 检查过的元素设置为1
vector<vector<int>> visited(h, vector<int>(w, 0));
//DFS启动
bool flag;
for (k = 0; k < coordinate.size() / 2; k++) {
flag = check(board, visited, word, coordinate[k * 2], coordinate[k * 2 + 1], 0, h, w);
if (flag) return true;
//把visited全部置为0
vector<vector<int>> visited(h, vector<int>(w, 0));
}
return false;
}
};
适当优化:
不在开辟过多的新visited 重复利用
对word数组采用引用
执行用时: 76 ms
内存消耗: 17 MB
class Solution {
public:
bool check(vector<vector<char>>& board, vector<vector<int>>& visited, string& word, int i, int j, int k, int h, int w) {
//i j是当前点的坐标 k是用于标记那个字符需要被查询
/* 两个逻辑 如果这个元素不是则回退 如果已经找到了则退出 */
//cout << "(" << i+1 << "," << j+1 << ")" << endl;
bool result = false;
if (board[i][j] != word[k]) {
return false;
}
else if (k == word.length() - 1) {
return true;
}
//设置visited
visited[i][j] = 1;
//找到了这个元素但是没有结束
k += 1;//找到了这个元素即可以自增
int temp_i;
int temp_j;
//设置偏移量
vector<pair<int, int>>direction{
{
0, 1},{
0 ,-1},{
1, 0},{
-1, 0} };//右 左 上 下
for (int n = 0; n < 4; n++) {
temp_i = i + direction[n].first;
temp_j = j + direction[n].second;
if (temp_i == h || temp_j == w || temp_i == -1 || temp_j == -1)continue;
if (!visited[temp_i][temp_j]) {
if (check(board, visited, word, temp_i, temp_j, k, h, w)) {
result = true;
break;
}
}
}
//经过四次循环发现找不到可以使用的元素 此时将其复原为0
visited[i][j] = 0;
return result;
}
bool exist(vector<vector<char>>& board, string word) {
int i, j, k;
int h = board.size(), w = board[0].size();
//获取开头第一个字母的位置 确定起始点
char star = word[0];//得到第一个字母
vector<int>coordinate;//储存坐标
//将坐标成功储存在coordinate中
for (i = 0; i < h; i++) {
for (j = 0; j < w; j++) {
if (board[i][j] == star) {
coordinate.push_back(i);
coordinate.push_back(j);
}
}
}
//初始化一个检查矩阵 检查过的元素设置为1
vector<vector<int>> visited(h, vector<int>(w, 0));
//DFS
bool flag;
for (k = 0; k < coordinate.size() / 2; k++) {
flag = check(board, visited, word, coordinate[k * 2], coordinate[k * 2 + 1], 0, h, w);
if (flag) return true;
//把visited全部置为0
}
return false;
}
};
再度优化:
添加private来存放一些数据加快运行
执行用时:24 ms, 在所有 C++ 提交中击败了93.89%的用户
内存消耗:8.8 MB, 在所有 C++ 提交中击败了44.96%的用户
class Solution {
private:
vector<pair<int, int>>direction{
{
0, 1},{
0 ,-1},{
1, 0},{
-1, 0} };//右 左 上 下
public:
bool check(vector<vector<char>>& board, vector<vector<int>>& visited, string& word, int i, int j, int k, int h, int w) {
//i j是当前点的坐标 k是用于标记那个字符需要被查询
/* 两个逻辑 如果这个元素不是则回退 如果已经找到了则退出 */
bool result = false;
if (board[i][j] != word[k]) {
return false;
}
else if (k == word.length() - 1) {
return true;
}
//设置visited
visited[i][j] = 1;
//找到了这个元素但是没有结束
k += 1;//找到了这个元素即可以自增
int temp_i;
int temp_j;
//设置偏移量
for (int n = 0; n < 4; n++) {
temp_i = i + direction[n].first;
temp_j = j + direction[n].second;
if (temp_i == h || temp_j == w || temp_i == -1 || temp_j == -1)continue;
if (!visited[temp_i][temp_j]) {
if (check(board, visited, word, temp_i, temp_j, k, h, w)) {
result = true;
break;
}
}
}
//经过四次循环发现找不到可以使用的元素 此时将其复原为0
visited[i][j] = 0;
return result;
}
bool exist(vector<vector<char>>& board, string word) {
int i, j, k;
int h = board.size(), w = board[0].size();
//获取开头第一个字母的位置 确定起始点
char star = word[0];//得到第一个字母
vector<int>coordinate;//储存坐标
//将坐标成功储存在coordinate中
for (i = 0; i < h; i++) {
for (j = 0; j < w; j++) {
if (board[i][j] == star) {
coordinate.push_back(i);
coordinate.push_back(j);
}
}
}
//初始化一个检查矩阵 检查过的元素设置为1
vector<vector<int>> visited(h, vector<int>(w, 0));
//DFS
bool flag;
for (k = 0; k < coordinate.size() / 2; k++) {
flag = check(board, visited, word, coordinate[k * 2], coordinate[k * 2 + 1], 0, h, w);
if (flag) return true;
//把visited全部置为0
}
return false;
}
};
边栏推荐
- Strong performance growth! Wentai technology's net profit in the first half of the year was 1.7 billion yuan, a sharp increase of 767.19% year-on-year!
- Digital filter (II) -- minimum phase delay system and all pass system
- 示波器参数详解
- Temporary URL
- 视频号从每周2-3场到每天3场
- 把MySQL8的数据库备份导入MySQL5版本中
- Ren Zhengfei's latest voice: American politicians hope Huawei will die, and the desire to survive inspires Huawei
- Tips -- solve the problem of no module named matlab.engine
- 高温天气户外活动有讲究!市民盛夏健身安全指引来了
- There is a special cryptology language called asn.1
猜你喜欢

USB Type-C 之CC线简介

About the difference between root and alias under localization

频谱仪原理简介二

Leetcode--45. jumping game II (greed)

Digital filter (V) -- design IIR filter

矢量网络分析仪(矢网)的校准

Openmv (III) -- get camera pictures in real time

频谱仪原理简介一

物联网在智慧城市的应用

The difference between probability function p (x), probability distribution function f (x) and probability density function f (x)
随机推荐
业绩增长强劲!闻泰科技上半年净利17亿元,同比暴增767.19%!
Digital filter (I) -- basic structure and matlab implementation of IIR and fir
通过公众号等私域渠道,为视频号直播引流
busybox最新版(busybox apk)
com.mysql.jdbc. Configuration files of driver and com.mysql.cj.jdbc.driver
美国对华为禁令开始波及欧洲芯片厂商
如何简简单单地自己动手磨刀
Openmv (VI) -- STM32 realizes object recognition and handwritten digit recognition
天线的原理、分类及要求
The difference between probability function p (x), probability distribution function f (x) and probability density function f (x)
Digital filter (III) -- Design of analog filter
Introduction to USB type-C PD fast charging
Openmv (III) -- get camera pictures in real time
“3·8女王节”限时大促竟让场观、视频号销量翻倍?
沪硅产业上半年营收8.5亿元,同比增长30.53%!各类产品认证正在加速
Electrotechnics Volume II self study notes 1.23
USB Type-C 详解
Answer questions about the pixel, resolution and size of the picture, as well as the display size of the monitor.
频谱分析仪的性能参数
Five key considerations for network security budget planning in 2023