当前位置:网站首页>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());
}
};
边栏推荐
- Android Sqlite3基本命令
- Unity插件:使用PopulationSystem制作行走交流的路人
- 四平方和,激光炸弹
- 代码随想录笔记_动态规划_1049最后一块石头的重量II
- [Opportunity Enlightenment-60]: "Soldiers, Stupid Ways"-1- Opening: "Death" and "Life" are the way of heaven
- token 过期后,如何自动续期?
- Oracle 数据库用户创建、重启、导入导出
- B.构造一个简单的数列(贪心)
- 1403. 非递增顺序的最小子序列
- 本周讨论用户体验:Daedalus 的 Nemo 加入 Ambire,探索加密海洋
猜你喜欢
Database recovery
leetcode:251. 展开二维向量
NPDP|作为产品经理,如何快速提升自身业务素养?
This week to discuss the user experience: Daedalus Nemo to join Ambire, explore the encryption of the ocean
16、学习MySQL 正则表达式
化繁为简,聊一聊复制状态机系统架构抽象
实际工作中的高级技术(训练加速、推理加速、深度学习自适应、对抗神经网络)
[Beiya data recovery] IBM System Storage storage lvm information lost data recovery solution
Sum of four squares, laser bombs
理论篇1:深度学习之----LetNet模型详解
随机推荐
【模型部署与业务落地】基于量化芯片的损失分析
数据库恢复
Why does the decimal point appear when I press the space bar in word 2003?
Google plug-in. Download contents file is automatically deleted after solution
xampp安装包含的组件有(php,perl,apche,mysql)
Technology sharing | Mini program realizes audio and video calls
metaRTC5.0新版本支持mbedtls(PolarSSL)
C# winforms 输入颜色转换颜色名
【历史上的今天】8 月 4 日:第一位图灵奖女性得主;NVIDIA 收购 MediaQ;首届网络安全挑战大赛完成
Crawler - action chain, xpath, coding platform use
Android Sqlite3 basic commands
关于redis的几件小事(五)redis保证高并发以及高可用
B.构造一个简单的数列(贪心)
This week to discuss the user experience: Daedalus Nemo to join Ambire, explore the encryption of the ocean
自监督学习未来是掩码自编码器?KAIST最新《自监督学习掩码自编码器》研究进展
Set partition minimum difference problem (01 knapsack)
Technology sharing | Description of the electronic fence function in the integrated dispatching system
vim common operation commands
RS|哨兵二号(.SAFE格式)转tif格式
化繁为简,聊一聊复制状态机系统架构抽象