当前位置:网站首页>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());
}
};
边栏推荐
- 职场漫谈:为什么越是内卷的行业越有人争着抢着往里冲?好奇怪的说...
- JCMsuite应用:倾斜平面波传播透过光阑的传输
- Problem solving-->Online OJ (18)
- 华为手机切换屏幕效果_华为p40页面切换效果怎么换
- Crawler - basic use of selenium, no interface browser, other uses of selenium, cookies of selenium, crawler cases
- idea removes spark logs
- MySQL【窗口函数】【共用表表达式】
- 在腾讯,我的试用期总结!
- 砺夏行动|九州云章津楠:开源不是少数人的运动,大众化才是源泉
- token 过期后,如何自动续期?
猜你喜欢
技术分享| 小程序实现音视频通话
谷歌插件.crx文件下载后被自动删除的解决方法
ICML 2022 | 图神经网络的局部增强
JCMsuite应用:倾斜平面波传播透过光阑的传输
Technology sharing | Description of the electronic fence function in the integrated dispatching system
化繁为简,聊一聊复制状态机系统架构抽象
Find My Technology | Prevent your pet from getting lost, Apple Find My technology can help you
Problem solving-->Online OJ (18)
Theory 1: Deep Learning - Detailed Explanation of the LetNet Model
实际工作中的高级技术(训练加速、推理加速、深度学习自适应、对抗神经网络)
随机推荐
Kyushu Cloud attended the Navigator Online Forum to discuss the current status, challenges and future of 5G MEC edge computing
异步编程概览
AOSP built-in APP franchise rights white list
[Beiya data recovery] IBM System Storage storage lvm information lost data recovery solution
CCF GLCC正式开营|九州云开源专家携丰厚奖金,助力高校开源推广
The Internet of things application development trend
eNSP-小型网络拓扑(DNS、DHCP、网站服务器、无线路由器)
阴影初始化【5】
leetcode:253. 至少需要多少间会议室
[LeetCode] 38. Appearance sequence
CF1527D MEX Tree(mex&树&容斥)
RS|哨兵二号(.SAFE格式)转tif格式
本周讨论用户体验:Daedalus 的 Nemo 加入 Ambire,探索加密海洋
如何和程序员谈恋爱
Crawler - action chain, xpath, coding platform use
How to install postgresql and configure remote access in ubuntu environment
编程思想_编程有必要给孩子学吗?
Technology sharing | Mini program realizes audio and video calls
杭电校赛(ACM组队安排)
一看就会的Chromedriver(谷歌浏览器驱动)安装教程