Leetcode79. Word Search单词搜索
给定一个二维网格和一个单词,找出该单词是否存在于网格中。
单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中“相邻”单元格是那些水平相邻或垂直相邻的单元格。同一个单元格内的字母不允许被重复使用。
示例:
board = [ ['A','B','C','E'], ['S','F','C','S'], ['A','D','E','E'] ] 给定 word = "ABCCED", 返回 true. 给定 word = "SEE", 返回 true. 给定 word = "ABCB", 返回 false.
class Solution {
public:
vector<vector<int> > visit;
int len;
int r;
int c;
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};
bool exist(vector<vector<char> >& board, string word)
{
len = word.size();
if(len == 0)
return true;
r = board.size();
if(r == 0)
return false;
c = board[0].size();
if(r * c < len)
return false;
for(int i = 0; i < r; i++)
{
for(int j = 0; j < c; j++)
{
visit.clear();
visit = vector<vector<int> >(r, vector<int>(c, 0));
if(board[i][j] == word[0])
{
visit[i][j] = 1;
if(DFS(board, 1, word, i, j))
return true;
}
}
}
return false;
}
bool DFS(vector<vector<char> >& board, int pos, string cmp, int x, int y)
{
if(pos >= cmp.size())
return true;
for(int i = 0; i < 4; i++)
{
int xx = x + dx[i];
int yy = y + dy[i];
if(xx < 0 || xx >= r || yy < 0 || yy >= c)
continue;
if(visit[xx][yy] == 1)
continue;
if(board[xx][yy] != cmp[pos])
continue;
visit[xx][yy] = 1;
if(DFS(board, pos + 1, cmp, xx, yy))
return true;
visit[xx][yy] = 0;
}
return false;
}
};
Leetcode79. Word Search单词搜索的更多相关文章
- [LeetCode] 79. 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 79. Word Search单词搜索 (C++)
题目: Given a 2D board and a word, find if the word exists in the grid. The word can be constructed fr ...
- 079 Word Search 单词搜索
给定一个二维面板和一个单词,找出该单词是否存在于网格中.这个词可由顺序相邻的单元格内的字母构成,其中“相邻”单元格是那些水平相邻或垂直相邻的单元格.同一个单元格内的字母不允许被重复使用.例如,给定 二 ...
- [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] 79. 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 l ...
- [LeetCode OJ] Word Search 深度优先搜索DFS
Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from l ...
- Leetcode79 Word Search
题目描述 Given a 2D board and a word, find if the word exists in the grid. The word can be constructed f ...
- [LeetCode] 212. Word Search II 词语搜索 II
Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...
随机推荐
- ERROR in xxx.js from UglifyJs
在打包项目的时候,出现如下的错误: 经过搜索找到原因: 这是因为webpack在打包vue文件时没有成功转换ES6的语法. 解决方案 解决方法很简单,加入babel-preset-es2015插件即可 ...
- js图片预加载实现!
var myImage = (function(){ var imgNode = document.createElement( 'img' ); document.body.appendChild( ...
- System.Web.Mvc.PartialViewResult.cs
ylbtech-System.Web.Mvc.PartialViewResult.cs 1.程序集 System.Web.Mvc, Version=5.2.3.0, Culture=neutral, ...
- Hibernate关联映射(一对多/多对多)
版权声明:翀版 https://blog.csdn.net/biggerchong/article/details/843401053. Hibernate关联映射上接Hibernate持久化类:h ...
- 第一周——clone项目到本地
公司使用的是git进行version control,代码托管在gitlab. 按照公司规范注册了gitlab账号, 漫长的等待clone到本地~ 然而,还是有问题,jar包下载不完全(公司网速dow ...
- 5行代码怎么实现Hadoop的WordCount?
初学编程的人,都知道hello world的含义,当你第一次从控制台里打印出了hello world,就意味着,你已经开始步入了编程的大千世界,这和第一个吃螃蟹的人的意义有点类似,虽然这样比喻并不恰当 ...
- OSG在VS2008下的配置安装
一.准备工作 下载相关的工具软件: 1, 最新版的OSG库:OpenSceneGraph-2.8.2.zip. 2, 安装源代码所需要的工具:cmake-2.6.4-win32-x86.zip 3, ...
- Reflections框架,类扫描工具
Reflections是一个能提供一站式服务的对象. 巧用Reflections库实现包扫描(扫描某个包中某个接口实现.注解等) 它扫描工程的classpath,为元数据建索引,允许你运行时查询元数据 ...
- 3、mysql读写性能优化方法
1.当表格特别多的时候,所新建的表格一定注意索引,数据库内部对索引的处理能够很好的优化查询读写性能
- 关于判断是安卓还是ios环境跳转下载页
H5项目中判断是安卓还是iOS手机就跳转到不同的下载页,项目如下https://github.com/JserJser/dailyPush/tree/master/daily6/H5 这个项目里面我比 ...