Given an m x n board of characters and a list of strings words, return all words on the board. Each word must be constructed from letters of sequentially adjacent cells, where adjacent cells are horizontally or vertically neighboring. The same letter cell may not be used more than once in a word.
     这个题目相比于word search来说不是对一个word进行检索,而是对多个word 进行检索,可以对在79题目的基础上进行修改 ,增加一个循环,得到一个基本的解法,但是时间复杂度较高,没法oc
class Solution {
public:
bool dfs(vector<vector<char>>& board,string word,int i,int j,int n){
//递归终止条件
if(n==word.size()) return true;
if(i<0 || j<0 || i>=board.size()||j>=board[0].size() || board[i][j]!=word[n]) return false;
board[i][j]='0';//这个位置检索到了
bool flag=(dfs(board,word,i+1,j,n+1) || dfs(board,word,i-1,j,n+1)||
dfs(board,word,i,j+1,n+1)|| dfs(board,word,i,j-1,n+1));
board[i][j]=word[n];
return flag;
}
vector<string> findWords(vector<vector<char>>& board, vector<string>& words) {
//word search plus版本 word search 是检索一个word 这个版本是每个word都进行检索
// 一个单词一个单词的去检索
int m=board.size(),n=board[0].size();
vector<string>res;
for(auto word:words){
for(int i=0;i<m;++i){
for(int j=0;j<n;++j){
if(dfs(board,word,i,j,0)){
res.push_back(word);
i=m;
j=n;//跳出循环
}
}
}
}
return res;
}
};
  如何优化这个题目呢,能不能并发的对多个word同时进行检索呢? 通过字典树来实现快速查询。
class Solution {
int dir[5]={1,0,-1,0,1};//检索方向
public:
//嵌套类 构建一个前缀树
class Trie{ public:
string s;
Trie *next[26]; public: Trie() {
s="";
memset(next,0,sizeof(next));//初始化多叉树的索引
} void insert(string word){
Trie *node=this;
for(auto ww:word){
if(node->next[ww-'a']==NULL){
node->next[ww-'a']=new Trie();
}
node=node->next[ww-'a'];
}
node->s=word;
}
}; vector<string> findWords(vector<vector<char>>& board, vector<string>& words) {
vector<string> res;
if(board.size()==0 || board[0].size()==0 || words.size()==0) return res;
vector<vector<bool>> dp(board.size(),vector<bool>(board[0].size(),false));
Trie *T=new Trie();
for(auto word:words){
T->insert(word);
}
for(int i=0;i<board.size();++i){
for(int j=0;j<board[0].size();++j){
if(T->next[board[i][j]-'a']!=NULL){
search(board,T->next[board[i][j]-'a'],i,j,dp,res);
}
}
}
return res;
}
void search(vector<vector<char>>& board, Trie* p, int i, int j, vector<vector<bool>>& dp, vector<string>& res) {
if (!p->s.empty()) {
res.push_back(p->s);
p->s.clear();
}
dp[i][j] = true;
for (int ii=0;ii<4;++ii) {
int nx = i + dir[ii], ny = j + dir[ii+1];
if (nx >= 0 && nx < board.size() && ny >= 0 && ny < board[0].size() && !dp[nx][ny] && p->next[board[nx][ny] - 'a']) {
search(board, p->next[board[nx][ny] - 'a'], nx, ny, dp, res);
}
}
dp[i][j] = false;
}
};
  同样也可以用结构体来构建一个前缀树:
class Solution {
public:
struct TrieNode {
TrieNode *child[26];
string str;
};
struct Trie {
TrieNode *root;
Trie() : root(new TrieNode()) {}
void insert(string s) {
TrieNode *p = root;
for (auto &a : s) {
int i = a - 'a';
if (!p->child[i]) p->child[i] = new TrieNode();
p = p->child[i];
}
p->str = s;
}
};
vector<string> findWords(vector<vector<char>>& board, vector<string>& words) {
vector<string> res;
if (words.empty() || board.empty() || board[0].empty()) return res;
vector<vector<bool>> visit(board.size(), vector<bool>(board[0].size(), false));
Trie T;
for (auto &a : words) T.insert(a);
for (int i = 0; i < board.size(); ++i) {
for (int j = 0; j < board[i].size(); ++j) {
if (T.root->child[board[i][j] - 'a']) {
search(board, T.root->child[board[i][j] - 'a'], i, j, visit, res);
}
}
}
return res;
}
void search(vector<vector<char>>& board, TrieNode* p, int i, int j, vector<vector<bool>>& visit, vector<string>& res) {
if (!p->str.empty()) {
res.push_back(p->str);
p->str.clear();
}
int d[][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
visit[i][j] = true;
for (auto &a : d) {
int nx = a[0] + i, ny = a[1] + j;
if (nx >= 0 && nx < board.size() && ny >= 0 && ny < board[0].size() && !visit[nx][ny] && p->child[board[nx][ny] - 'a']) {
search(board, p->child[board[nx][ny] - 'a'], nx, ny, visit, res);
}
}
visit[i][j] = false;
}
};

【leetcode】212. Word Search II的更多相关文章

  1. 【LeetCode】212. Word Search II 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 前缀树 日期 题目地址:https://leetco ...

  2. 【LeetCode】79. Word Search

    Word Search Given a 2D board and a word, find if the word exists in the grid. The word can be constr ...

  3. 【LeetCode】140. Word Break II

    Word Break II Given a string s and a dictionary of words dict, add spaces in s to construct a senten ...

  4. 【LeetCode】140. Word Break II 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归求解 日期 题目地址:https://leetc ...

  5. 【LeetCode】79. Word Search 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 回溯法 日期 题目地址:https://leetco ...

  6. [leetcode trie]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】126. Word Ladder II

    题目如下: 解题思路:DFS或者BFS都行.本题的关键在于减少重复计算.我采用了两种方法:一是用字典dic_ladderlist记录每一个单词可以ladder的单词列表:另外是用dp数组记录从star ...

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

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

  9. 【LeetCode】Longest Word in Dictionary through Deleting 解题报告

    [LeetCode]Longest Word in Dictionary through Deleting 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode. ...

随机推荐

  1. DeWeb 简介

    DeWeb是一个可以直接将Delphi程序快速转换为网页应用的工具! 使用DeWeb, 开发者不需要学习HTML.JavaScript.Java.PHP.ASP.C#等新知识,用Delphi搞定一切. ...

  2. HashSet的remove方法(一道面试题)

    1 public class CollectionTest { 2 3 @Test 4 public void test3(){ 5 HashSet set = new HashSet(); 6 Pe ...

  3. MySQL怎么缓解读的压力的?---buffer pool

    每当我们想要缓解读,一般会想到什么? 预读取,缓存 缓存 缓存,其实就是将高频访问的数据放到内存里面,减少读盘的次数. 为了提高内存的利用率,MySQL还建立了缓存池,也就是buffer pool,存 ...

  4. Linux下软链接与硬链接的区别

    由于下面会说到inode,所以如果没有了解过,请务必搞懂inode的真正含义,厚颜无耻的推荐我的一篇博客:Linux磁盘与文件系统管理 如果我们在系统中新建一个文件,我们看到的文件名实际上只是表面现象 ...

  5. VMware Workstation 无法连接到虚拟机。请确保您有权运行该程序、访问该程序使用的所有目录以及访问所有临时文件目录。 VMware Authorization Service 当前未运行

    VMware Workstation 无法连接到虚拟机.请确保您有权运行该程序.访问该程序使用的所有目录以及访问所有临时文件目录. VMware Authorization Service 当前未运行 ...

  6. Fastjson妙用之@JSONField注解

    在开发的过程中使用json格式的地方非常多,现在前后端分离的项目中,前后端数据交换的格式一般为json,这种格式的优/缺点这里不再赘述,感兴趣的可以百度.把java中的实体类序列化为json的方式也有 ...

  7. python 函数的定义及调用语法,map 方法,函数嵌套递归

    1.什么是函数    开发程序时候,需要代码执行多次,为了提高编写效率及代码重用性,所以把具有独立功能的代码块组织为一个小模块,给这个功能一个名称,这就是函数.    函数可以使用系统自带的函数也可以 ...

  8. 【Sass/SCSS】预加载器中的“轩辕剑”

    [Sass/SCSS]预加载器中的"轩辕剑" 博客说明 文章所涉及的资料来自互联网整理和个人总结,意在于个人学习和经验汇总,如有什么地方侵权,请联系本人删除,谢谢! 说明 随着前端 ...

  9. [loj2478]林克卡特树

    原题等价于选择恰好$k+1$条不相交(无公共点)的路径使得边权和最大 证明:对于原题中的最优解,一定包含了k条0边权的边(否则可以将未使用的边删掉,然后将这条路径的末尾与不在同一个连通块内的点连边), ...

  10. HarmonyOS 3.0.0开发者预览版全新发布

    2021年10月22日在华为开发者大会HDC.Together 2021 主题演讲上,我们发布了HarmonyOS 3.0.0开发者预览版,主要内容包括:Harmony设计系统.ArkUI 3.0.A ...