当前位置:网站首页>leetcode:212. 单词搜索 II
leetcode:212. 单词搜索 II
2022-08-04 14:31:00 【OceanStar的学习笔记】
题目来源
题目描述


题目解析
前缀和 + 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());
}
};
边栏推荐
- Lecture 4 SVN
- word2003按空格键为什么会出现小数点
- 【历史上的今天】8 月 4 日:第一位图灵奖女性得主;NVIDIA 收购 MediaQ;首届网络安全挑战大赛完成
- token 过期后,如何自动续期?
- 基于 Next.js实现在线Excel
- [机缘参悟-60]:《兵者,诡道也》-1-开篇:“死“与“生“都是天道
- Bluetooth Technology|In the first half of the year, 1.3 million charging piles were added nationwide, and Bluetooth charging piles will become the mainstream of the market
- 开发者独立搭建一个跨模态搜索应用有多难?
- 文盘Rust -- 配置文件解析
- 如何才能有效、高效阅读?猿辅导建议“因材因时施教”
猜你喜欢

快解析结合千方百剂

【剑指offer59】队列的最大值

Google plug-in. Download contents file is automatically deleted after solution

开放麒麟 openKylin 版本规划敲定:10 月发布 0.9 版并开启公测,12 月发布 1.0 版

代码随想录笔记_动态规划_1049最后一块石头的重量II

SLAM 04.视觉里程计-1-相机模型

MySQL【触发器】

【历史上的今天】8 月 4 日:第一位图灵奖女性得主;NVIDIA 收购 MediaQ;首届网络安全挑战大赛完成

Database recovery

Win11快速助手在哪里?Win11打开快速助手的方法
随机推荐
AOSP内置APP特许权限白名单
Makefile 语法及使用笔记
技术分享| 小程序实现音视频通话
Almost all known protein structures in the world are open sourced by DeepMind
郑轻新生校赛和中工选拔赛题解
编程思想_编程有必要给孩子学吗?
物联网应用发展趋势
华为手机切换屏幕效果_华为p40页面切换效果怎么换
利用决策树找出最优特征组合
考研上岸又转行软件测试,从5k到13k完美逆袭,杭州校区小哥哥拒绝平庸终圆梦!
蓝牙技术|上半年全国新增 130 万台充电桩,蓝牙充电桩将成为市场主流
四平方和,激光炸弹
Rust 从入门到精通04-变量
【Web技术】1401- 图解 Canvas 入门
Technology sharing | Description of the electronic fence function in the integrated dispatching system
[Opportunity Enlightenment-60]: "Soldiers, Stupid Ways"-1- Opening: "Death" and "Life" are the way of heaven
F. Jinyu and its outer matrix (construction)
[Beiya data recovery] IBM System Storage storage lvm information lost data recovery solution
Redis 复习计划 - Redis主从数据一致性和哨兵机制
信创是什么意思?涉及哪些行业?为什么要发展信创?