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. 六. Go并发编程--WaitGroup

    一. 序言 WaitGroup是Golang应用开发过程中经常使用的并发控制技术. WaitGroup,可理解为Wait-Goroutine-Group,即等待一组goroutine结束.比如某个go ...

  2. Django(73)django-debug-toolbar调试工具

    介绍 Django框架的调试工具栏使用django-debug-toolbar库,是一组可配置的面板,显示有关当前请求/响应的各种调试信息,点击时,显示有关面板内容的更多详细信息. 应用 1. 安装 ...

  3. Django 前端BootCSS 实现分页

    通过使用bootstrap框架,并配合Django自带的Paginator分页组件即可实现简单的分页效果. 1.创建MyWeb项目 python manage.py startapp MyWeb 2. ...

  4. 【java+selenium3】select 下拉选 (八)

    一.select 下拉框 1.下拉选的处理类:Select 如果页面元素是一个下拉框,我们可以将此web元素封装成Select对象. Select select = new Select(WebEle ...

  5. aardio 开发桌面应用,这几点必须要掌握!

    1. 前言 大家好,我是安果! 上一篇文章写到可以通过 aardio 结合 Python 开发桌面应用,有些小伙伴后台给我留言,说 Aardio 资料太少,希望我能补充一些实用的功能 实用 | 利用 ...

  6. 开源项目|Go 开发的一款分布式唯一 ID 生成系统

    原文连接: 开源项目|Go 开发的一款分布式唯一 ID 生成系统 今天跟大家介绍一个开源项目:id-maker,主要功能是用来在分布式环境下生成唯一 ID.上周停更了一周,也是用来开发和测试这个项目的 ...

  7. 使用 SSL 加密的 JDBC 连接 SAP HANA 数据库

    近期客户为满足安全要求,提了让业务应用使用 SSL 方式连接 SAP HANA 数据库的需求.本人查询 SAP官方文档 发现数据库支持 SSL 连接,有参数直接加到 JDBC 的 URL 后边就行了, ...

  8. C# 计算农历日期方法(2021版)

    解决问题 旧版农历获取方法报错,会有 到 2021年 m数组越界了 if (LunarData[m] < 4095) 此方法可以解决 主体代码 public static class China ...

  9. pytest-mian函数运行

    1.设置多并发运行 1.命令行安装 pip install pytest-xdist #安装插件,进行多并发运行,#调用:-n -5 import pytest # pytest.main([&quo ...

  10. 04373 C++程序设计 2019版 第一章习题五、程序设计题

    题目: 1.编写一个程序,将从键盘输入的n个字符串保存在一个一维数组A中.在输入字符串之前,先输入n的值.要求,数组A需要动态申请空间,程序运行结束前再释放掉. #include <iostre ...