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. Leetcode注意

    List<List<Integer>> res = new ArrayList<>();

  2. JVM虚拟机—JVM的垃圾回收机制(转载)

    1.前言 理解JVM的垃圾回收机制(简称GC)有什么好处呢?作为一名软件开发者,满足自己的好奇心将是一个很好的理由,不过更重要的是,理解GC工作机制可以帮助你写出更好的Java程序. 在学习GC前,你 ...

  3. DBMS_MONITOR程序开启10046事件

    在具有连接池或共享服务器的多层环境中,一个会话可以跨越多个进程,甚至跨越多个实例.DBMS_MONITOR是在Oracle 10g中引入的内置的程序包,通过该程序包可以跟踪从客户机到中间层.再到后端数 ...

  4. python16_day09【Select多路复用】

    一.select多路复用 句柄列表11, 句柄列表22, 句柄列表33 = select.select(句柄序列1, 句柄序列2, 句柄序列3, 超时时间) 参数: 可接受四个参数(前三个必须) 返回 ...

  5. nodejs多核处理

    前言大家都知道nodejs是一个单进程单线程的服务器引擎,不管有多么的强大硬件,只能利用到单个CPU进行计算.所以,有人开发了第三方的cluster,让node可以利用多核CPU实现并行. 随着nod ...

  6. sqlserver索引的原理及索引建立的注意事项小结

    聚集索引,数据实际上是按顺序存储的,数据页就在索引页上.就好像参考手册将所有主题按顺序编排一样.一旦找到了所要搜索的数据,就完成了这次搜索,对于非聚集索引,索引是安全独立于数据本身结构的,在索引中找到 ...

  7. 广度优先搜索 BFS算法

    广度优先搜索算法(Breadth-First-Search,BFS),又称作宽度优先搜索.BFS算法是从根节点开始,沿着树的宽度遍历树的节点.如果所有节点均被访问,则算法中止. 算法思想 1.首先将根 ...

  8. SQLMAP 使用手册

    当给sqlmap这么一个url的时候,它会: 1.判断可注入的参数 2.判断可以用那种SQL注入技术来注入 3.识别出哪种数据库 4.根据用户选择,读取哪些数据 sqlmap支持五种不同的注入模式: ...

  9. Which HTTP methods match up to which CRUD methods?

    https://stackoverflow.com/questions/6203231/which-http-methods-match-up-to-which-crud-methods   Crea ...

  10. install tabix/bgzip

    bgzip – Block compression/decompression utility tabix – Generic indexer for TAB-delimited genome pos ...