leetcode 热题100(208. 实现 Trie (前缀树))数组模拟c++

链接:208. 实现 Trie (前缀树) - 力扣(LeetCode)

Tire(发音类:似 "try")或者说 前缀树 是一种树形数据结构,用于高效地存储和检索字符串数据集中的键。这一数据结构有相当多的应用情景,例如自动补全和拼写检查。

请你实现 Trie 类:

  • Trie() 初始化前缀树对象。
  • void insert(String word) 向前缀树中插入字符串 word 。
  • boolean search(String word) 如果字符串 word 在前缀树中,返回 true(即,在检索之前已经插入);否则,返回 false 。
  • boolean startsWith(String prefix) 如果之前已经插入的字符串 word 的前缀之一为 prefix ,返回 true ;否则,返回 false 。

示例:

复制代码
输入
["Trie", "insert", "search", "search", "startsWith", "insert", "search"]
[[], ["apple"], ["apple"], ["app"], ["app"], ["app"], ["app"]]
输出
[null, null, true, false, true, null, true]

解释
Trie trie = new Trie();
trie.insert("apple");
trie.search("apple");   // 返回 True
trie.search("app");     // 返回 False
trie.startsWith("app"); // 返回 True
trie.insert("app");
trie.search("app");     // 返回 True

提示:

  • 1 <= word.length, prefix.length <= 2000
  • word 和 prefix 仅由小写英文字母组成
  • insert、search 和 startsWith 调用次数 总计 不超过 3 * 104 次

题意

很明显,题目要我们实现一个trie树的查找数据结构

思路

直接数据模拟trie树数据结构,定义cnt数组来记录哪个位置出现过一个完整的单词,查找前缀的话,只要出现过的直接返回true,否则直接返回false,插入的话直接插入,模拟下一个层即可

代码

cpp 复制代码
class Trie {
public:
    int idx = 0;
    int cnt[300010]={0};    //定义cnt来记录当前位置是一个完整的单词
    int trie[300010][30];   //定义trie数组,最多调用3*10*4个数据,说明可能最大层为这么大,然后的30是定义的26个字母 0-25
    Trie() {
        
    }
    
    void insert(string word) {
        int p = 0;
        for(auto c:word){
            int val = c-'a';    //转变为0-25
            if(!trie[p][val]) trie[p][val]=++idx;   //在当前层数未出现
            p = trie[p][val];   //转为下一层
        }
        //cout<<"insert::" <<p<<endl;
        cnt[p]++;   //记录当前层的位置为一个完整单词
    }
    
    bool search(string word) {
        int p = 0;
        for(auto c:word){
            int val = c-'a';
            if(!trie[p][val]) return false; //找不到下一个位置的字母,直接返回false
            p = trie[p][val];
        }
        //cout<<word<<" "<<"search::"<<p<<endl;
        return cnt[p];  //判断当前位置是否是一个完整单词
    }
    
    bool startsWith(string prefix) {
        int p = 0;
        for(auto c:prefix){
            int val = c-'a';
            if(!trie[p][val]) return false;
            p = trie[p][val];
        }
        //没有查不到,说明是某个单词的前缀
        return true;    
    }
};

/**
 * Your Trie object will be instantiated and called as such:
 * Trie* obj = new Trie();
 * obj->insert(word);
 * bool param_2 = obj->search(word);
 * bool param_3 = obj->startsWith(prefix);
 */
相关推荐
tangguofeng12 分钟前
基于分治策略的循环赛日程表问题的思路讲解
算法
起个名字费劲死了35 分钟前
VisionMaster集成深度学习算法(基础狗)
人工智能·深度学习·算法
lengxuenong38 分钟前
枚举例题分析
c++·算法·青少年编程
Navigator_Z1 小时前
LeetCode //C - 1269. Number of Ways to Stay in the Same Place After Some Steps
c语言·算法·leetcode
2601_965742221 小时前
短视频脚本创作方法分享:本地生活类账号的起步思路
大数据·人工智能·算法·ai·新媒体运营·生活
55873生态系统手记1 小时前
读完 55873 资产钱包的技术栈,我重新理解了 "合规钱包" 应该长什么样
算法·架构
旋生万物1 小时前
素数螺旋映射 $z_n=n^{1+i}$ 的角分布统计检验与零模型对比
大数据·前端·人工智能·算法·云原生·螺旋生成论·螺旋相位
HRTOS1 小时前
HRTOS Shell继续完善:优化printf缓冲区与字符串越界保护,国庆后随新版本更新
运维·单片机·嵌入式硬件·算法·51单片机
码艺-Alimjan1 小时前
Web 网站打包桌面应用的另一种方式,超级简单(C# exe 33Kb)
开发语言·前端·c#
邵奈一2 小时前
当沉香之乡遇上 Seed-2.1-pro:我用 AI 给村子做了一个数字门面
算法·架构