当前位置:网站首页>leetcode: 212. Word Search II
leetcode: 212. Word Search II
2022-08-04 14:36:00 【OceanStar study notes】
题目来源
题目描述


题目解析
前缀和 + DFS
注意:
- 可能出现: hf、 hfks(易错点)
class Solution {
struct TrieNode{
int size; // 可能出现不止一次
std::string path;
std::vector<TrieNode *> children;
TrieNode() : size(0), children(26, NULL){
}
};
void add(TrieNode *root, std::string &word){
TrieNode *node = root;
for(char ch : word){
int idx = ch - 'a';
if(node->children[idx] == nullptr){
node->children[idx] = new TrieNode();
}
node = node->children[idx];
}
node->size ++;
node->path = word;
}
std::set<std::string> set;
bool process(vector<vector<char>>& board, int i, int j, TrieNode *node){
int N = board.size(), M = board[0].size();
if(i < 0 || j < 0 || i == N || j == M || board[i][j] == 0 || node->children[ board[i][j] - 'a'] == nullptr){
return false;
}
int idx = board[i][j] - 'a';
node = node->children[idx];
if(node->size > 0){
node->size--; // 易错点......
set.emplace(node->path);
} //可能还没有结束,不能提前返回
board[i][j] = 0;
bool ans = false;
if(process(board, i + 1, j, node)
|| process(board, i - 1, j, node)
|| process(board, i, j + 1, node)
|| process(board, i, j - 1, node)){
ans = true;
}
board[i][j] = idx + 'a';
return ans;
}
public:
vector<string> findWords(vector<vector<char>>& board, vector<string>& words) {
TrieNode *root = new TrieNode();
for(auto w : words){
add(root, w);
}
for (int i = 0; i < board.size(); ++i) {
for (int j = 0; j < board[i].size(); ++j) {
TrieNode *node = root;
process(board, i, j, node);
}
}
return std::vector<std::string> (set.begin(), set.end());
}
};
边栏推荐
- 从理论到实践:MySQL性能优化和高可用架构,一次讲清
- ACL 2022 | 社会科学理论驱动的言论建模
- 砺夏行动|九州云章津楠:开源不是少数人的运动,大众化才是源泉
- 世间几乎所有已知蛋白质结构,都被DeepMind开源了
- 【Today in History】August 4: First female Turing Award winner; NVIDIA acquires MediaQ; first Cybersecurity Challenge completed
- 量化细胞内的信息流:机器学习时代下的研究进展
- vim common operation commands
- 编程思想_编程有必要给孩子学吗?
- How to automatically renew the token after it expires?
- C# 动态加载卸载 DLL
猜你喜欢

【HMS core】【Media】【视频编辑服务】 在线素材无法展示,一直Loading状态或是网络异常

Kyushu Cloud attended the Navigator Online Forum to discuss the current status, challenges and future of 5G MEC edge computing

电子行业MES管理系统有哪些特殊功能

16、学习MySQL 正则表达式

九州云出席领航者线上论坛,共话5G MEC边缘计算现状、挑战和未来

CCF GLCC officially opened | Kyushu Cloud open source experts bring generous bonuses to help universities promote open source

Sum of four squares, laser bombs

期货开户之前要谈好最低手续费和交返

X射线掠入射聚焦反射镜

《中国综合算力指数》《中国算力白皮书》《中国存力白皮书》《中国运力白皮书》在首届算力大会上重磅发出
随机推荐
CF1527D MEX Tree(mex&树&容斥)
leetcode:259. 较小的三数之和
Technology sharing | Mini program realizes audio and video calls
【 HMS core 】 【 Media 】 online video editing service 】 【 material can't show, or network anomalies have been Loading state
本周讨论用户体验:Daedalus 的 Nemo 加入 Ambire,探索加密海洋
AlphaFold 如何实现 AI 在结构生物学中的全部潜力
代码随想录笔记_动态规划_1049最后一块石头的重量II
快解析结合友加畅捷U+
理论篇1:深度学习之----LetNet模型详解
OAID是什么
leetcode:241. 为运算表达式设计优先级
特殊品种的二次开户验资金额
leetcode:253. 至少需要多少间会议室
在腾讯,我的试用期总结!
Rust from entry to proficient 04-variables
Lixia Action | Kyushu Yunzhang Jinnan: Open source is not a movement for a few people, popularization is the source
【问题解决】QT更新组件出现 “要继续此操作,至少需要一个有效且已启用的储存库”
Android Sqlite3基本命令
用了TCP协议,就一定不会丢包吗?
阿里老鸟终于把测试用例怎么写说的明明白白了,小鸟必看