当前位置:网站首页>力扣79单词搜索
力扣79单词搜索
2022-06-26 03:25:00 【瀛台夜雪】
力扣79单词搜索
题目详情
给定一个 m x n 二维字符网格 board 和一个字符串单词 word 。如果 word 存在于网格中,返回 true ;否则,返回 false 。
单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中“相邻”单元格是那些水平相邻或垂直相邻的单元格。同一个单元格内的字母不允许被重复使用。
输入输出样例
输入:board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "ABCCED"
输出:true
输入:board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "SEE"
输出:true
输入:board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "ABCB"
输出:false
解法:回溯算法
bool exist(vector<vector<char>>&board,string word)
{
int rLength=board.size();
int cLength=board[0].size();
//建立二维数组,用来表示位置是否被访问过,数组大小与board的大小需要一致
vector<vector<int>>visitesd(rLength,vector<int>(cLength));
for(int i=0;i<rLength;i++)
{
for(int j=0;j<cLength;j++)
{
//判断是否属于单词搜索
if(check(board,visitesd,i,j,word,0))
{
return true;
}
}
}
return false;
}
//k指代当前是word的第几个字符
bool check(vector<vector<char>>&board,vector<vector<int>>&visited,int i,int j,string word,int k)
{
if(board[i][j]!=word[k])
{
return false;
}
else if(k==word.size()-1)
{
return true;
}
//当word[k]在board中存在,但并未到字符串末尾时
//1.首先将标志位设置为true
visited[i][j]=true;
//建立方向数组,标记可以移动的方向
vector<pair<int,int>>direction{
{
0,1},{
0,-1},{
1,0},{
-1,0}};
//标记周围是否存在相同的字符串
bool res=false;
for(auto dir:direction)
{
//newi,newj 代表新的坐标值
int newi=i+dir.first;
int newj=j+dir.second;
//判断newi和newj是否超出边界或则不存在
if(newi>=0&&newi<board.size()&&newj>=0&&newj<board[0].size())
{
//当该值并没有被访问过
if(!visited[newi][newj])
{
//从里到外的循环
bool flag=check(board,visited,newi,newj,word,k+1);
if(flag)
{
//当下一个字符存在的时候便跳出循环
res=true;
break;
}
}
}
}
//重置标记位
visited[i][j]=false;
return res;
}
边栏推荐
- Preparation for wechat applet development
- Dynamic segment tree leetcode seven hundred and fifteen
- . Net core learning journey
- Contains an object field at offset position
- Cloud Computing Foundation -0
- 解决uniapp插件robin-editor设置字体颜色和背景颜色报错的问题
- js实现文字跑马灯效果
- 进度条
- Restful API interface design standards and specifications
- 点击事件
猜你喜欢
Nepal graph learning Chapter 3_ Multithreading completes 6000w+ relational data migration
progress bar
机器学习笔记 - 时间序列的趋势分量
解决uniapp插件robin-editor设置字体颜色和背景颜色报错的问题
Non H5 end of uni app, regional setting of status bar on the top of mobile phone
After Ali failed to start his job in the interview, he was roast by the interviewer in the circle of friends (plug)
进度条
Classic model - Nin & googlenet
Camera-memory内存泄漏分析(三)
Some mobile phones open USB debugging, and the solution to installation failure
随机推荐
Some mobile phones open USB debugging, and the solution to installation failure
Todolist incomplete, completed
Tupu software is the digital twin of offshore wind power, striving to be the first
Preparation for wechat applet development
[paper notes] supersizing self supervision: learning to grasp from 50K tries and 700 robot hours
Multimedia elements, audio, video
[hash table] improved, zipper hash structure - directly use two indexes to search, instead of hashing and% every time
【Appium踩坑】io.appium.uiautomator2.common.exceptions.InvalidArgumentException: ‘capabilities‘ are mand
mysql存储过程
todolist未完成,已完成
Upload file / text / picture, box shadow
智能制造学习记录片和书籍
解决uniapp插件robin-editor设置字体颜色和背景颜色报错的问题
阿里云函数计算服务一键搭建Z-Blog个人博客
Deletelater Usage Summary in QT
进程之间的通信方式
链路监控 pinpoint
ABP framework Practice Series (II) - Introduction to domain layer
Navicat16 wireless trial
Where is it safe to open a fund account?