[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 ...
随机推荐
- (转)PHP中extract()函数的妙用
近日在看一个牛人的代码时,看到一个非常好用的函数:extract(),它的主要作用是将数组展开,键名作为变量名,元素值为变量值,可以说为数组的操作提供了另外一个方便的工具,比方说,可以很方便的提取$_ ...
- 第十二章作业 MemoryBugs-master项目优化笔记
作业要求: 下载bug项目:https://github.com/lzyzsd/MemoryBugs,请注意配合使用MemoryMonitor, AllocationTracker以及HeapDump ...
- emmt html生成
html:5 或 ! html:5 或!:用于HTML5文档类型 html:xt:用于XHTML过渡文档类型 html:4s:用于HTML4严格文档类型 常用过渡文档类型 html:xt 直接c ...
- 31.Spring-开发流程.md
[toc] 1.简单开发流程 1.1引用类库 基本类库: ## 1.2创建spring配置文件,文件的名称为固定格式:applicationContext.xml或者bean.xml: <?xm ...
- jsp中button传值
onclick=location.href("linker.jsp?custno="+ from1.custno.value)或者onClick ="a()" ...
- linux下 oracle常用命令
打开图形化窗口: 1)Database Configuration Assistant windows (添加数据库实例) $ dbca 2)Oracle Net Configuration A ...
- asp.net 调用天气所遇到的问题
由于在项目用了显示天气的功能,原有的调用方法 直接通过 <iframe name="weather_inc" src="http://i.tianqi.c ...
- HTML5 QQ登录背景动态图片
预览效果如图所示: 代码如下: <!DOCTYPE html> <head> <meta http-equiv="Content-Type" cont ...
- php字符串常见面试题
>> 本文固定链接: http://php.ncong.com/mianshi/mianshiti_string.html >> 转载请注明: 恩聪php 2014年09月02 ...
- MYSQL管理之主从同步管理 转载
MYSQL主从同步架构是目前使用最多的数据库架构之一,尤其是负载比较大的网站,因此对于主从同步的管理也就显得非常重要,新手往往在出现主从同步错误的时候不知道如何入手,这篇文章就是根据自己的经验来详细叙 ...