【leetcode】212. Word Search II
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;
}
};
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的更多相关文章
- 【LeetCode】212. Word Search II 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 前缀树 日期 题目地址:https://leetco ...
- 【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 ...
- 【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 ...
- 【LeetCode】140. Word Break II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归求解 日期 题目地址:https://leetc ...
- 【LeetCode】79. Word Search 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 回溯法 日期 题目地址:https://leetco ...
- [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 ...
- 【leetcode】126. Word Ladder II
题目如下: 解题思路:DFS或者BFS都行.本题的关键在于减少重复计算.我采用了两种方法:一是用字典dic_ladderlist记录每一个单词可以ladder的单词列表:另外是用dp数组记录从star ...
- leetcode 79. Word Search 、212. Word Search II
https://www.cnblogs.com/grandyang/p/4332313.html 在一个矩阵中能不能找到string的一条路径 这个题使用的是dfs.但这个题与number of is ...
- 【LeetCode】Longest Word in Dictionary through Deleting 解题报告
[LeetCode]Longest Word in Dictionary through Deleting 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode. ...
随机推荐
- hdu 2086 A1 = ? (公式推导)
有如下方程:Ai = (Ai-1 + Ai+1)/2 - Ci (i = 1, 2, 3, .... n).若给出A0, An+1, 和 C1, C2, .....Cn.请编程计算A1 = ? Inp ...
- 一次fork引发的惨案!
"你还有什么要说的吗?没有的话我就要动手了",kill程序最后问道. 这一次,我没有再回答. 只见kill老哥手起刀落,我短暂的一生就这样结束了··· 我是一个网络程序,一直以来都 ...
- .Net Minimal Api 介绍
Minimal API是.Net 6中新增的模板,借助C# 10的一些特性以最少的代码运行一个Web服务.本文脱离VS通过VS Code,完成一个简单的Minimal Api项目的开发. 创建项目 随 ...
- @RequestAttribute与@MatrixVariable
@RequestAttribute 它只能使用在方法入参上,从request请求参数中获取对应的属性值. //路径跳转 @GetMapping("/goto") public St ...
- FZU ICPC 2020 寒假训练 4 —— 模拟(一)
P1042 乒乓球 题目背景 国际乒联现在主席沙拉拉自从上任以来就立志于推行一系列改革,以推动乒乓球运动在全球的普及.其中11分制改革引起了很大的争议,有一部分球员因为无法适应新规则只能选择退役.华华 ...
- 问题 K: 找点
题目描述 上数学课时,老师给了LYH一些闭区间,让他取尽量少的点,使得每个闭区间内至少有一个点.但是这几天LYH太忙了,你们帮帮他吗? 输入 多组测试数据. 每组数据先输入一个N,表示有N个闭区间(N ...
- 问题 B: 喷水装置(二)(在c++上运行有错误,提交AC了)
题目描述 有一块草坪,横向长w,纵向长为h,在它的橫向中心线上不同位置处装有n(n<=10000)个点状的喷水装置,每个喷水装置i喷水的效果是让以它为中心半径为Ri的圆都被润湿.请在给出的喷水装 ...
- 华为开发者大会主题演讲:3D建模服务让内容高效生产
内容来源:华为开发者大会2021 HMS Core 6 Graphics技术论坛,主题演讲<3D建模服务使能3D内容高效生产>. 演讲嘉宾:华为消费者云服务 AI算法专家 3D建模服务(3 ...
- [atAGC034E]Complete Compress
先考虑枚举最后的点,并以其为根 首先,操作祖先-后代关系是没有意义的,因为以后必然有一次操作会操作祖先使其返回原来的位置,那么必然不如操作后代和那一个点(少一次操作) 考虑某一次操作,总深度和恰好减2 ...
- [luogu5204]Train Tracking 2
考虑一个位置的上界,即$bi=min(c_{i-k+1},c_{i-k+2},--,ci)$,那么每一个位置有两种方式:1.达到上界:2.未达到上界那么可以将权值相同的ci和bi提出来,由于权值不同的 ...