字典树也叫前缀树、Trie树等 字典树是一颗非典型的多叉树模型 字典树的结点包含有一个长度为26的指针数组,分别对应26个字母,指向当前字母对应的下一个字母。 字典树充分利用了字符串的公共前缀 包含三个单词 “sea”,“sells”,“she” 的字典树如下所示: 图片来源
定义
struct Trie
{
vector children;
string word;
Trie() : children(26), word("") {}
};
插入
void insert(Trie *root, string word)
{
Trie *node = root;
for (char c : word)
{
c -= 'a';
if (node->children[c] == nullptr)
node->children[c] = new Trie();
node = node->children[c];
}
node->word = word;
}
查找
bool search(Trie *root, string word)
{
Trie *node = root;
for (char c : word)
{
c -= 'a';
if (node->letter[c] == nullptr)
return false;
node=node->letter[c];
}
if (node->word != "")
return true;
else
return false;
}
前缀查找
bool startsWith(Trie *root,string prefix)
{
Trie *node = root;
for (char c : prefix)
{
c -= 'a';
if (node->letter[c] == nullptr)
return false;
node=node->letter[c];
}
return true;
}
例题
LeetCode 208 Trie(发音类似 “try”)或者说 前缀树 是一种树形数据结构,用于高效地存储和检索字符串数据集中的键。这一数据结构有相当多的应用情景,例如自动补完和拼写检查。 请你实现 Trie 类: Trie() 初始化前缀树对象。 void insert(String word) 向前缀树中插入字符串 word 。 boolean search(String word) 如果字符串 word 在前缀树中,返回 true(即,在检索之前已经插入);否则,返回 false 。 boolean startsWith(String prefix) 如果之前已经插入的字符串 word 的前缀之一为 prefix ,返回 true ;否则,返回 false 。
class Trie
{
private:
vector letter;
bool isend = false;
public:
Trie():letter(26), isend(false)
{
}
void insert(string word)
{
Trie *node = this;
for (char c : word)
{
c -= 'a';
if (node->letter[c] == nullptr)
node->letter[c] = new Trie();
node = node->letter[c];
}
node->isend = true;
}
bool search(string word)
{
Trie *node = this;
for (char c : word)
{
c -= 'a';
if (node->letter[c] == nullptr)
return false;
node=node->letter[c];
}
if (node->isend)
return true;
else
return false;
}
bool startsWith(string prefix)
{
Trie *node = this;
for (char c : prefix)
{
c -= 'a';
if (node->letter[c] == nullptr)
return false;
node=node->letter[c];
}
return true;
}
};
LeetCode 212 给定一个 m x n 二维字符网格 board 和一个单词(字符串)列表 words,找出所有同时在二维网格和字典中出现的单词。 单词必须按照字母顺序,通过 相邻的单元格 内的字母构成,其中“相邻”单元格是那些水平相邻或垂直相邻的单元格。同一个单元格内的字母在一个单词中不允许被重复使用。 示例: 输入:board = [[“o”,“a”,“a”,“n”],[“e”,“t”,“a”,“e”],[“i”,“h”,“k”,“r”],[“i”,“f”,“l”,“v”]], words = [“oath”,“pea”,“eat”,“rain”] 输出:[“eat”,“oath”]
class Solution
{
public:
int dirs[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
vector ret;
struct Trie
{
vector children;
string word;
Trie() : children(26), word("") {}
};
void insert(Trie *root, string word)
{
Trie *node = root;
for (char c : word)
{
int i = c - 'a';
if (node->children[i] == nullptr)
node->children[i] = new Trie();
node = node->children[i];
}
node->word = word;
}
void dfs(int x, int y, Trie *node, vector &board)
{
char c = board[x][y];
if (node->children[c - 'a'] == nullptr)
return;
board[x][y] = '#';
node = node->children[c - 'a'];
if (node->word != "")
{
ret.push_back(node->word);
node->word="";
}
for (int i = 0; i = 0 && newx = 0 && newy
关注
打赏
最近更新
- 深拷贝和浅拷贝的区别(重点)
- 【Vue】走进Vue框架世界
- 【云服务器】项目部署—搭建网站—vue电商后台管理系统
- 【React介绍】 一文带你深入React
- 【React】React组件实例的三大属性之state,props,refs(你学废了吗)
- 【脚手架VueCLI】从零开始,创建一个VUE项目
- 【React】深入理解React组件生命周期----图文详解(含代码)
- 【React】DOM的Diffing算法是什么?以及DOM中key的作用----经典面试题
- 【React】1_使用React脚手架创建项目步骤--------详解(含项目结构说明)
- 【React】2_如何使用react脚手架写一个简单的页面?