Leetcode#79 Word Search
依次枚举起始点,DFS+回溯
代码:
bool dfs(vector<vector<char> > &board, int r, int c, string word) {
int m = board.size();
int n = board[].size();
int dir[][] = {{-, }, {, }, {, -}, {, }}; if (word.empty())
return true; if (r >= && r < m && c >= && c < n && board[r][c] == word[]) {
for (int i = ; i < ; i++) {
char tmp = board[r][c];
board[r][c] = ;
if (dfs(board, r + dir[i][], c + dir[i][], word.substr()))
return true;
board[r][c] = tmp;
}
} return false;
} bool exist(vector<vector<char> > &board, string word) {
if (board.empty() || board[].empty()) return false; int m = board.size();
int n = board[].size(); for (int i = ; i < m; i++)
for (int j = ; j < n; j++)
if (dfs(board, i, j, word))
return true; return false;
}
Leetcode#79 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 词语搜索
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 、212. Word Search II
https://www.cnblogs.com/grandyang/p/4332313.html 在一个矩阵中能不能找到string的一条路径 这个题使用的是dfs.但这个题与number of is ...
- 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(单词查找)
题目链接:https://leetcode.com/problems/word-search/#/description 给出一个二维字符表,并给出一个String类型的单词,查找该单词是否出现在该二 ...
- 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 ...
- [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 ...
- 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 ...
- 刷题79. Word Search
一.题目说明 题目79. Word Search,给定一个由字符组成的矩阵,从矩阵中查找一个字符串是否存在.可以连续横.纵找.不能重复使用,难度是Medium. 二.我的解答 惭愧,我写了很久总是有问 ...
随机推荐
- TableViewCell自适应高度
//初始化TableView时设置 self.tv.estimatedRowHeight=54;self.tv.rowHeight=UITableViewAutomaticDimension;
- iOS开发那些事-iOS6苹果地图实用开发
在iOS 6之后,不再使用谷歌地图了,而是使用苹果自己的地图,但是API编程接口没有太大的变化.开发人员不需要再学习很多新东西就能开发地图应用,这是负责任的做法.因此本节介绍的内容也同样适用于iOS5 ...
- iOS访问通讯录开发-读取联系人信息
读取通信录中的联系人一般的过程是先查找联系人记录,然后再访问记录的属性,属性又可以分为单值属性和多值属性.通过下面例子介绍联系人的查询,以及单值属性和多值属性的访问,还有读取联系人中的图片数据. 本案 ...
- .NET中导入导出Excel总结
前一段时间,做了Excle的导入和导出,在此记录开发思路及技术要点,以便在今后开发中参考. ...
- 利用CSS3打造一组质感细腻丝滑的按钮
CSS3引入了众多供功能强大的新特性,让设计和开发人员能够轻松的创作出各种精美的界面效果.下面这些发出闪亮光泽的按钮,漂亮吧?把鼠标悬停在按钮上,还有动感的光泽移动效果. 温馨提示:为保证最佳的效果, ...
- frame和iframe区别
1.frame不能脱离frameSet单独使用,iframe可以: 2.frame不能放在body中:如下可以正常显示: <!--<body>--> <frameset ...
- Amazon Kinesis Producer Library 使用记录
Amazon Kinesis 是一种托管的服务,用于有弹性与扩展性的实时处理大规模的流数据.该服务收集大数据记录流,多个可在 Amazon EC2 实例上运行的数据处理应用程序随后可实时使用此流. 在 ...
- SQL中char,varchar,nvarchar等的异同
比较这几个数据类型,总是忘记,可能比较细节的原因.先做个记号,回头完善.
- [转]Linux下修改/设置环境变量JAVA_HOME
1. 永久修改,对所有用户有效 # vi /etc/profile //按键盘[Shift + g], 在profile文件最后添加下面的内容: export JAVA_HOME = /home/m ...
- (转)Android Support Percent百分比布局
一.概述 周末游戏打得过猛,于是周天熬夜码代码,周一早上浑浑噩噩的发现 android-percent-support-lib-sample(https://github.com/JulienGeno ...