[LeetCode] Word Search [37]
题目
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.
For example,
Given board =
[
["ABCE"],
["SFCS"],
["ADEE"]
]
word = "ABCCED",
-> returns true,
word = "SEE",
-> returns true,
word = "ABCB",
-> returns false.
解题思路
给一个二维字符数组,给一个字符串,问该二维数组是否包括该字符串。比方一个二维数组[ ABCE SFCS ADEE ]和字符串"ABCCED",这个就包括。解决问题,基本的关键是怎么解决在二维数组中查找方向,怎样来标识哪些是走过的。
代码实现
class Solution {
public:
bool exist(vector<vector<char> > &board, string word) {
int m = board.size();
if(m<=0) return false;
int n = board[0].size();
for(int i=0; i<m; ++i)
for(int j=0; j<n; ++j){
if(helper(0, word, i, j, board))
return true;
}
return false;
}
bool helper(int k, const string &word, int i, int j, vector<vector<char> > &board){
if(k==word.size()-1 && board[i][j]==word[k])
return true;
if(board[i][j] != word[k])
return false;
char temp = board[i][j];
// 走过的地方使用 '.' 来表示
board[i][j] = '.';
bool b1=false, b2=false, b3=false, b4=false;
// board[i][j]的上面
if(i>0 && board[i-1][j]!='.')
b1 = helper(k+1, word, i-1, j, board);
// board[i][j]的以下
if(!b1 && i<board.size()-1 && board[i+1][j] != '.')
b2 = helper(k+1, word, i+1, j, board);
// board[i][j]的左面
if(!b1 && !b2 && j>0 && board[i][j-1] != '.')
b3 = helper(k+1, word, i, j-1, board);
// board[i][j]的右面
if(!b1 && !b2 && !b3 && j<board[0].size()-1 && board[i][j+1]!='.')
b4 = helper(k+1, word, i, j+1, board);
board[i][j] = temp;
return b1 || b2 || b3 || b4;
}
};
另外,我开通了微信公众号--分享技术之美,我会不定期的分享一些我学习的东西.
)
[LeetCode] Word Search [37]的更多相关文章
- [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 ...
- LeetCode: Word Search 解题报告
Word SearchGiven a 2D board and a word, find if the word exists in the grid. The word can be constru ...
- LeetCode() Word Search II
超时,用了tire也不行,需要再改. class Solution { class TrieNode { public: // Initialize your data structure here. ...
- [leetcode]Word Search @ Python
原题地址:https://oj.leetcode.com/problems/word-search/ 题意: Given a 2D board and a word, find if the word ...
- [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 待解决?
终于搞定了这个DFS,最近这个DFS写的很不顺手,我一直以为递归这种东西只是在解重构时比较麻烦,现在看来,连最简单的返回true和false的逻辑关系都不能说one hundred present 搞 ...
- [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 ...
随机推荐
- phonegap 2.8.1 toast
目录结构如下: 以上三个用红色框勾出的地方是需要修改的文件夹. 首先:添加java代码. 在src目录下新建一个包裹:org.apache.cordova 在该包裹下新建类:ToastPlugin.j ...
- Binary XML file : Error inflating class com.esri.android.map.MapView
在测试esri arcgis for android的第一个程序Helloworld的时候,报这样的错: Binary XML file : Error inflating class com.esr ...
- SQL利用临时表实现动态列、动态添加列
--方法一--------------------------------------------------------------------- declare @sql as varchar(1 ...
- javaScript 手写图片轮播
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- [转]mysql 的日志的启动与查看
mysql有以下几种日志:错误日志: -log-err查询日志: -log慢查询日志: -log-slow-queries更新日志: -log-update二进制日志:-log-bin 日志 ...
- Hibernate 性能优化之懒加载
针对数据库中的大数据,不希望特别早的加载到内存中,当用到它的时候才加载 懒加载分为:类的懒加载.集合的懒加载.单端关联的懒加载 类的懒加载 1.在默认情况下,类就是执行懒加载 2. ...
- Python list 常用操作
测试版本: python 2.7 获取第一个.最后一个元素 list1 = ["a", "b", "c"] len1 = len(list1 ...
- jquery利用event.which方法获取键盘输入值的代码
jquery利用event.which方法获取键盘输入值的代码,需要的朋友可以参考下. 实例 显示按了哪个键: $("input").keydown(function(event) ...
- javascript获得给定日期的前一天的日期
/** * 获得当前日期的前一天 */ function getYestoday(date){ var yesterday_milliseconds=date.getTime()-1000*60*60 ...
- [PHP学习日志]简单Session的使用
首先,给出一些Session的解释:目前最实用的网络协议即HTTP超文本传输协议,它是“无状态”的,所谓“无状态”是指它在用户与服务器交互时没有存储需要交互的“状态”.而Session 是在网络应用中 ...