Leetcode79 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.
Example:
board =
[
['A','B','C','E'],
['S','F','C','S'],
['A','D','E','E']
]
Given word = "ABCCED", return true.
Given word = "SEE", return true.
Given word = "ABCB", return false.
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/word-search
代码实现
class Solution {
public:
bool existCore(vector<vector<char>>& board, string &word,int & Wordpos,int rows,int cols,int row,int col,vector<vector<int> > & visited){
if(Wordpos>=word.size())
return true;
if(col>=cols||row>=rows||col<0||row<0){
return false;
}
bool flag=false;
if(board[row][col]==word[Wordpos]&& visited[row][col]!=1){
++Wordpos;
visited[row][col]=1;
flag=existCore(board,word,Wordpos,rows,cols,row+1,col,visited)||existCore(board,word,Wordpos,rows,cols,row,col+1,visited)||existCore(board,word,Wordpos,rows,cols,row-1,col,visited)||existCore(board,word,Wordpos,rows,cols,row,col-1,visited);
if(!flag)
{
--Wordpos;
visited[row][col]=0;
}
}
return flag;
}
bool exist(vector<vector<char>>& board, string word) {
bool result=false;
if(board.size()==0||board[0].size()==0){
return false;
}
int rows=board.size();
int cols=board[0].size();
vector<vector<int> > visited;
for(int i=0;i<rows;++i){
vector<int> temp;
for(int j=0;j<cols;++j){
temp.push_back(0);
}
visited.push_back(temp);
}
int Wordpos=0;
for(int i=0;i<rows;++i){
for(int j=0;j<cols;++j)
{
if(existCore(board,word,Wordpos,rows,cols,i,j,visited))
return true;
}
}
return false;
}
};
总结
思路就是回溯法。中间纠结了一下,visited的数组是用二维数组还是用vector。后来发现如果用二维数组,传指针的地方较为麻烦,而且不能像vector一样直接visited[row][col]。
Leetcode79 Word Search的更多相关文章
- Leetcode79. Word Search单词搜索
给定一个二维网格和一个单词,找出该单词是否存在于网格中. 单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中"相邻"单元格是那些水平相邻或垂直相邻的单元格.同一个单元格内的字 ...
- [LeetCode] Word Search II 词语搜索之二
Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...
- [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
July 6, 2015 Problem statement: Word Search Given a 2D board and a word, find if the word exists in ...
- Word Search I & II
Word Search I Given a 2D board and a word, find if the word exists in the grid. The word can be cons ...
- 【leetcode】Word Search
Word Search Given a 2D board and a word, find if the word exists in the grid. The word can be constr ...
- Java for LeetCode 212 Word Search II
Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...
- 51. Word Search
Word Search Given a 2D board and a word, find if the word exists in the grid. The word can be constr ...
- 79. 212. Word Search *HARD* -- 字符矩阵中查找单词
79. Word Search Given a 2D board and a word, find if the word exists in the grid. The word can be co ...
随机推荐
- requests--传递参数
传递参数 传递URL参数 data = {'city': '北京'} # 参数有中文如果发送不了,必须要编码 city = parse.urlencode(data).encode('utf-8') ...
- IOI 2013 袋熊(线段树+分块+决策单调性)
题意 http://www.ioi2013.org/wp-content/uploads/tasks/day1/wombats/Wombats%20zh%20(CHN).pdf 思路 我们设矩形的 ...
- Ubuntu 14.04 apt-get update失效解决(转)
现象如下: VirtualBox:~$ sudo apt-get update Err http://mirrors.aliyun.com trusty InRelease Err http://mi ...
- [原创]A/B测试系统调研思维导图
[原创]A/B测试系统调研思维导图
- Unity Shader 广告牌效果
广告牌效果指的是,一个二维平面的法线方向始终与视线(摄像机的观察方向)相同.广泛运用于渲染烟雾,云朵,闪光等. 它的本质在于构建旋转矩阵,此时我们可以选择三个基向量来构建此矩阵. 指向→的方向(X轴) ...
- SQL -------- WHERE子句与AND,OR和NOT运算符结合使用。
AND, OR and NOT 与 运算符中的且或非的意思相同 WHERE子句可以与AND,OR和NOT运算符结合使用. and 表示 查询的语句必须全部包含and 连接的两个或多个条件 or ...
- K8S的DNS服务
k8s集群部署完后第一件事就是要配置DNS服务,目前可选的方案有skydns, kube-dns, coredns kube-dns是k8s中的一个内置插件,目前作为一个独立的开源项目维护,见http ...
- C#下载csv代码总结
一.C#导出csv格式代码如下: /// <summary> /// 下载 /// </summary> /// <param name="startTime& ...
- Redis主从复制看这篇就够了
一.概念和作用 概念:主机数据更新后根据配置和策略, 自动同步到备机的master/slaver机制,Master以写为主,Slave以读为主. 作用: 读写分离: 容灾恢复: 二.复制原理 slav ...
- http 307重定向
刚才在做hexo页面优化,发现了本地测试返回http 307.以前没见过这个响应码,于是做一下调研. 相关文章: hexo页面优化 http 307 在rfc规范中,http 307 Temporar ...