[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 ...
随机推荐
- java中集合类的简介
结构 collection(接口) List(接口) LinkedList(类) ArrayList(类) Vector(类) Stack(类) Set(接口) Map(接口) Hashtable(类 ...
- XML样本(格式没区别,但是一个有结果 一个没结果)
xml样本01<orderlist> <order> <orderid>1</orderid> <ord ...
- js监听滚动条事件
(function () { if(document.addEventListener){ document.addEventListener('mousewheel',scrollFunc,fals ...
- 读书笔记 |Google C++编程风格指南
Google C++编程风格指南 ## 0. 背景 每一个C++程序员都知道,C++具有很多强大的语言特性,但这种强大不可避免的导致它的复杂,这种复杂会使得代码更易于出现bug.难于阅读和维护. 本指 ...
- mysql操作1
一.连接MYSQL.格式: mysql -h主机地址 -u用户名 -p用户密码1.连接到本机上的MYSQL.首先打开DOS窗口,然后进入目录mysql\bin,再键入命令mysql -u root - ...
- MVC3中 ViewBag、ViewData和TempData的使用和区别(转发:汴蓝)
MVC3中 ViewBag.ViewData和TempData的使用和区别 在MVC3开始,视图数据可以通过ViewBag属性访问,在MVC2中则是使用ViewData.MVC3中保留了ViewD ...
- Nginx源码研究四:NGINX的内存管理
关于nginx的内存使用,我们先看代码,下面是nginx_cycle.c中对全局数据结构cycle的初始化过程 pool = ngx_create_pool(NGX_CYCLE_POOL_SIZE, ...
- 如何让div出现滚动条
由于DIV本身属性并没有滚动条设置,但是有些地方的设计却需要出现滚动条,如何实现呢?本人采用CSS样式来控制显示!而且也很简单,代码如下: <div style="OVERFLOW-Y ...
- Xcode7调试-b
Xcode7中苹果为我们增加了两个重要的debug相关功能.了解之后觉得非常实用,介绍给大家. 1.Address Sanitizer: 妈妈再也不用担心 EXC_BAD_ACCESS? EXC_BA ...
- 关于如何在C语言中嵌入汇编命令
转载自:http://www.keil.com/support/docs/2308.htm C51: GETTING INLINE ASSEMBLY TO WORK Information in th ...