当前位置:网站首页>力扣(LeetCode)212. 单词搜索 II(2022.07.31)
力扣(LeetCode)212. 单词搜索 II(2022.07.31)
2022-08-01 04:36: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;
}
};
边栏推荐
- 怀念故乡的面条
- 【kali-信息收集】枚举——DNS枚举:DNSenum、fierce
- Optional parameters typescript19 - object
- 数组问题之《两数之和》以及《三数之和 》
- 今日睡眠质量记录68分
- Immutable
- 2022-07-31: Given a graph with n points and m directed edges, you can use magic to turn directed edges into undirected edges, such as directed edges from A to B, with a weight of 7.After casting the m
- Flink 1.13 (8) CDC
- Introduction to the Elastic Stack
- 博客系统(完整版)
猜你喜欢

typescript27-枚举类型呢

IJCAI2022 | Hybrid Probabilistic Reasoning with Algebraic and Logical Constraints

Interview Blitz 69: Is TCP Reliable?Why?

High Numbers | 【Re-integration】Line Area Score 880 Examples
![[Getting Started Tutorial] Rollup Module Packager Integration](/img/1d/c6f1afe894b326be6939a7e8783972.jpg)
[Getting Started Tutorial] Rollup Module Packager Integration

Make your Lottie support word wrapping in text fields

typescript28-枚举类型的值以及数据枚举

typescript25 - type assertion

基于ProXmoX VE的虚拟化家庭服务器(篇一)—ProXmoX VE 安装及基础配置

Introduction to the Elastic Stack
随机推荐
【愚公系列】2022年07月 Go教学课程 025-递归函数
API设计笔记:pimpl技巧
Message Queuing Message Storage Design (Architecture Camp Module 8 Jobs)
7月编程排行榜来啦!这次有何新变化?
2022-07-31: Given a graph with n points and m directed edges, you can use magic to turn directed edges into undirected edges, such as directed edges from A to B, with a weight of 7.After casting the m
Invalid classes inferred from unique values of `y`. Expected: [0 1 2], got [1 2 3]
深圳某游戏研发公司给每个工位都装监控,网友:堪比坐牢!
认真对待每一个时刻
Introduction to the Elastic Stack
typescript26-字面量类型
UE4 模型OnClick事件不生效的两种原因
TIM登陆时提示00001(TIM00001)
产品经理访谈 | 第五代验证码的创新与背景
MLP neural network, GRNN neural network, SVM neural network and deep learning neural network compare and identify human health and non-health data
7 行代码搞崩溃 B 站,原因令人唏嘘!
TypeScript simplifies running ts-node
请问shake数据库中想把源的db0的数据同步到目的db5,参数怎么设置呢?
Step by step hand tearing carousel Figure 3 (nanny level tutorial)
The maximum quantity leetcode6133. Grouping (medium)
MySQL3