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 =

[
['A','B','C','E'],
['S','F','C','S'],
['A','D','E','E']
]

word = "ABCCED", -> returns true,
word = "SEE", -> returns true,
word = "ABCB", -> returns false.

简单的单词查找,在上下左右四个方向想dfs就可以了,代码如下,注意函数之间传递引用否则会超时的:

 class Solution {
public:
bool exist(vector<vector<char>>& board, string word) {
if(!word.size())
return false;
int szHor = board.size();
if(!szHor) return false;
int szVer = board[].size();
if(!szVer) return false;
vector<vector<bool>> check(szHor, vector<bool>(szVer, false));
for(int i = ; i < szHor; ++i){
for(int j = ; j < szVer; ++j){
if(board[i][j] == word[]){
check[i][j] = true;
if(word.size() == || search(word.substr(), i, j, check, board))
return true;
check[i][j] = false;
}
}
}
return false;
} bool search(string word, int i, int j, vector<vector<bool>> & check, vector<vector<char>> & board)
{
vector<vector<char>> direction{{-, }, {, }, {, -}, {, }};
for(int k = ; k < ; ++k){
int ii = direction[k][] + i;
int jj = direction[k][] + j;
if(ii >= && ii < board.size() &&
jj >= && jj < board[].size() &&
board[ii][jj] == word[] &&
check[ii][jj] == false){
check[ii][jj] = true;
if(word.size() == || search(word.substr(), ii, jj, check, board))
return true;
check[ii][jj] = false;
}
}
return false;
}
};

LeetCode OJ:Word Search(单词查找)的更多相关文章

  1. [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 ...

  2. [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 ...

  3. 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 ...

  4. [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 ...

  5. 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 ...

  6. [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 ...

  7. leetcode 79. Word Search 、212. Word Search II

    https://www.cnblogs.com/grandyang/p/4332313.html 在一个矩阵中能不能找到string的一条路径 这个题使用的是dfs.但这个题与number of is ...

  8. [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 ...

  9. [LeetCode] 127. Word Ladder 单词阶梯

    Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest t ...

  10. [LeetCode] 139. Word Break 单词拆分

    Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine ...

随机推荐

  1. 在windows上搭建redis集群

    一 所需软件 Redis.Ruby语言运行环境.Redis的Ruby驱动redis-xxxx.gem.创建Redis集群的工具redis-trib.rb 二 安装配置redis redis下载地址   ...

  2. python学习之路-第七天-python面向对象编程简介

    面向对象编程 在python中,同样是类和对象作为重要的组成部分. 而且在python中基本数据类型如int都是有封装类的,都有自己的方法,应该是和java里面的Integer类似吧 类包括域和方法: ...

  3. 关于shared pool的深入探讨(二)【转载】

    关于shared pool的深入探讨(二)   作者:eygle |English [转载时请标明出处和作者信息]|[恩墨学院 OCM培训传DBA成功之道]链接:http://www.eygle.co ...

  4. Codeforces Round #396 (Div. 2) A - Mahmoud and Longest Uncommon Subsequence B - Mahmoud and a Triangle

    地址:http://codeforces.com/contest/766/problem/A A题: A. Mahmoud and Longest Uncommon Subsequence time ...

  5. Linux 搭建 SVN

    一.yum 安装 subversion yum -y install subversion 二.创建svn版本库所在路径(建议放在opt.usr.home下) mkdir -p /usr/local/ ...

  6. HTML5统计图表数据初始动画

    在线演示 本地下载

  7. 分布式集群Session原理及实现共享

    1.什么是Session/Cookie? 用户使用网站的服务,基本上需要浏览器与Web服务器的多次交互.HTTP协议本身是无状态的,当用户的第一次访问请求结束后,后端服务器就无法知道下一次来访问的还是 ...

  8. D3学习之地图

    D3学习之地图 (2017.03.09-03.11) 地图的意义 在可视化领域中,将数据点投影和关联到地理区域上,是一个非常关键的内容(体现了可视化中利用读者自身知识常识从而加速吸收信息的原则). G ...

  9. JavaWeb HTML

    1. HTML介绍 1.1. 什么是HTML HTML的全称为Hyper Text Markup Language,译为超文本标记语言. 超文本,就是指页面内可以包含图片.链接,甚至音乐.程序等非文字 ...

  10. “玲珑杯”ACM比赛 Round #13 B -- 我也不是B(二分排序)

    题意:开始有一个空序列s,一个变量c=0,接着从左往右依次将数组a中的数字放入s的尾部,每放一个数字就检测一次混乱度K,当混乱度k大于M时就清空序列并让c=c+1 K = Bi * Vi(1<= ...