当前位置:网站首页>Power button (LeetCode) 212. The word search II (2022.07.31)
Power button (LeetCode) 212. The word search II (2022.07.31)
2022-08-01 04:50:00 【ChaoYue_miku】
给定一个 m x n 二维字符网格 board 和一个单词(字符串)列表 words, 返回所有二维网格上的单词 .
单词必须按照字母顺序,通过 相邻的单元格 内的字母构成,其中“相邻”单元格是那些水平相邻或垂直相邻的单元格.同一个单元格内的字母在一个单词中不允许被重复使用.
示例 1:
输入:board = [[“o”,“a”,“a”,“n”],[“e”,“t”,“a”,“e”],[“i”,“h”,“k”,“r”],[“i”,“f”,“l”,“v”]], words = [“oath”,“pea”,“eat”,“rain”]
输出:[“eat”,“oath”]
示例 2:
输入:board = [[“a”,“b”],[“c”,“d”]], words = [“abcb”]
输出:[]
提示:
m == board.length
n == board[i].length
1 <= m, n <= 12
board[i][j] 是一个小写英文字母
1 <= words.length <= 3 * 104
1 <= words[i].length <= 10
words[i] 由小写英文字母组成
words 中的所有字符串互不相同
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/word-search-ii
方法一:回溯+字典树
C++提交内容:
struct TrieNode {
string word;
unordered_map<char,TrieNode *> children;
TrieNode() {
this->word = "";
}
};
void insertTrie(TrieNode * root,const string & word) {
TrieNode * node = root;
for (auto c : word){
if (!node->children.count(c)) {
node->children[c] = new TrieNode();
}
node = node->children[c];
}
node->word = word;
}
class Solution {
public:
int dirs[4][2] = {
{
1, 0}, {
-1, 0}, {
0, 1}, {
0, -1}};
bool dfs(vector<vector<char>>& board, int x, int y, TrieNode * root, set<string> & res) {
char ch = board[x][y];
if (!root->children.count(ch)) {
return false;
}
root = root->children[ch];
if (root->word.size() > 0) {
res.insert(root->word);
}
board[x][y] = '#';
for (int i = 0; i < 4; ++i) {
int nx = x + dirs[i][0];
int ny = y + dirs[i][1];
if (nx >= 0 && nx < board.size() && ny >= 0 && ny < board[0].size()) {
if (board[nx][ny] != '#') {
dfs(board, nx, ny, root,res);
}
}
}
board[x][y] = ch;
return true;
}
vector<string> findWords(vector<vector<char>> & board, vector<string> & words) {
TrieNode * root = new TrieNode();
set<string> res;
vector<string> ans;
for (auto & word: words){
insertTrie(root,word);
}
for (int i = 0; i < board.size(); ++i) {
for (int j = 0; j < board[0].size(); ++j) {
dfs(board, i, j, root, res);
}
}
for (auto & word: res) {
ans.emplace_back(word);
}
return ans;
}
};
边栏推荐
- Risk strategy important steps of tuning method
- 【无标题】
- MySQL实践总结-
- 在沈自所的半年总结
- 25. Have you been asked these three common interview questions?
- Excel record of integer programming optimization model to solve the problem
- 程序员代码面试指南 CD15 生成窗口最大值数组
- The difference between scheduleWithFixedDelay and scheduleAtFixedRate
- 请问shake数据库中为什么读取100个collection 后,直接就退出了,不继续读了呢?
- typescript21 - Comparison of Interfaces and Type Aliases
猜你喜欢
【愚公系列】2022年07月 Go教学课程 025-递归函数
MLP neural network, GRNN neural network, SVM neural network and deep learning neural network compare and identify human health and non-health data
(2022牛客多校四)H-Wall Builder II(思维)
mysql中解决存储过程表名通过变量传递的方法
高数 | 【重积分】线面积分880例题
云服务器下载安装mongo数据库并远程连接详细图文版本(全)
【目标检测】YOLOv7理论简介+实践测试
7月编程排行榜来啦!这次有何新变化?
Risk strategy important steps of tuning method
typescript24-类型推论
随机推荐
Risk strategy important steps of tuning method
UE4 从鼠标位置射出射线检测
25. 这三道常见的面试题,你有被问过吗?
Lawyer Interpretation | Guns or Roses?Talking about Metaverse Interoperability from the Battle of Big Manufacturers
MySQL-数据操作-分组查询-连接查询-子查询-分页查询-联合查询
UE4 制作遇到的问题
MySQL-数据定义语言-DDLdatebase define language
最新 955 不加班的公司名单
「以云为核,无感极速」顶象第五代验证码
Dry goods!How to Construct SRv6-TE Performance Test Environment Using Instrumentation
/etc/fstab
【云原生之kubernetes实战】kubernetes集群的检测工具——popeye
UE4 rays flashed from mouse position detection
"ArchSummit: The cry of the times, technical people can hear"
typescript26-字面量类型
【愚公系列】2022年07月 Go教学课程 023-Go容器之列表
PMP 相关方管理必背总结
leetcode:126. Word Solitaire II
Progressive Reconstruction of Visual Structure for Image Inpainting 论文笔记
High Numbers | 【Re-integration】Line Area Score 880 Examples