力扣211. 添加与搜索单词 - 数据结构设计

字典树

  • 思路:
    • 设计一棵字典树,每个节点存放单词的一个字符,节点放一个标记位,如果是单词结束则标记;
    • 字典树插入:
      • 字典树默认有 26 个 slot 槽代表 a - z;
      • 遍历单词,如果字符对应槽存在则迭代到子节点,如果不存在则创建;
      • 在单词结尾的节点,将 flag 标记;
    • 字典树查询:
      • 定义 dfs(word, index, trie) 函数,表示 word 的第 index 字符是否在 trie 树上;
      • 递归查询,终止条件为 index 为 word 长度,并且 flag 标记被设置;
      • 需要注意通配符 '.',代表任意字母:
        • } else if (c == '.') {

        •     for (int i = 0; i < 26; i++) {

        •         TrieNode* child = node->child[i];

        •         if (child != nullptr && dfs(word, index + 1, child)) {

        •             return true;

        •         }

        •     }

        • }

  • 完整代码:
struct TrieNode {
    std::vector<TrieNode*> child;
    bool isEnd;

    TrieNode() :
      child(std::vector<TrieNode*>(26, nullptr)) ,
      isEnd(false) {
    }

};

void insert(TrieNode* root, const std::string& word) {
    TrieNode* node = root;
    for (auto c : word) {
        if (node->child[c - 'a'] == nullptr) {
            node->child[c - 'a'] = new TrieNode();
        }
        node = node->child[c - 'a'];
    }
    node->isEnd = true;
}

class WordDictionary {
public:
    WordDictionary() {
        tire = new TrieNode();
    }
    
    void addWord(string word) {
        insert(tire, word);
    }
    
    bool search(string word) {
        return dfs(word, 0, tire);
    }

private:
    bool dfs(const std::string& word, int index, TrieNode* node) {
        if (index == word.size()) {
            return node->isEnd;
        }

        char c = word[index];
        if (c >= 'a' && c <= 'z') {
            TrieNode* child = node->child[c - 'a'];
            if (child != nullptr && dfs(word, index + 1, child)) {
                return true;
            }
        } else if (c == '.') {
            for (int i = 0; i < 26; i++) {
                TrieNode* child = node->child[i];
                if (child != nullptr && dfs(word, index + 1, child)) {
                    return true;
                }
            }
        }

        return false;
    }

private:
    TrieNode* tire;
};

/**
 * Your WordDictionary object will be instantiated and called as such:
 * WordDictionary* obj = new WordDictionary();
 * obj->addWord(word);
 * bool param_2 = obj->search(word);
 */
  • 后续将会对 zmq 中字典树的应用进行分析,敬请期待 ...

————————————————————————————

相关推荐

  1. LeetCode 211.添加搜索单词 - 数据结构设计 题解

    2024-01-24 14:04:04       62 阅读
  2. 211. 添加搜索单词 - 数据结构设计

    2024-01-24 14:04:04       73 阅读
  3. 添加搜索单词 - 数据结构设计[中等]

    2024-01-24 14:04:04       60 阅读
  4. 212题:单词搜索 II

    2024-01-24 14:04:04       25 阅读
  5. 79. 单词搜索

    2024-01-24 14:04:04       55 阅读
  6. 201. 数字范围按位(Python3)

    2024-01-24 14:04:04       68 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-01-24 14:04:04       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-24 14:04:04       100 阅读
  3. 在Django里面运行非项目文件

    2024-01-24 14:04:04       82 阅读
  4. Python语言-面向对象

    2024-01-24 14:04:04       91 阅读

热门阅读

  1. 蒙特卡洛方法概述

    2024-01-24 14:04:04       54 阅读
  2. Golang中int, int8, int16, int32, int64和uint区别

    2024-01-24 14:04:04       51 阅读
  3. 02_正则表达式的应用

    2024-01-24 14:04:04       49 阅读
  4. Flowable使用docker中MySQL8,Springboot启动出错

    2024-01-24 14:04:04       56 阅读
  5. el-select选项过多导致页面卡顿,路由跳转卡顿

    2024-01-24 14:04:04       49 阅读
  6. 机器的世界模型与人类的世界模型

    2024-01-24 14:04:04       50 阅读
  7. 【Spring Boot 3】【JPA】枚举类型持久化

    2024-01-24 14:04:04       50 阅读
  8. ES6笔记-symbol

    2024-01-24 14:04:04       50 阅读
  9. 最小生成树 prim + kruskal

    2024-01-24 14:04:04       44 阅读
  10. NLP自然语言处理介绍

    2024-01-24 14:04:04       50 阅读
  11. 2024.1.20 Python学习笔记7:字符串常见处理函数

    2024-01-24 14:04:04       51 阅读
  12. C++中模板的使用

    2024-01-24 14:04:04       56 阅读