美文网首页
前缀树 LC208

前缀树 LC208

作者: 锦绣拾年 | 来源:发表于2021-04-19 22:14 被阅读0次

前缀树就是一个拥有26个指针的链表
打算实现C++和python 版本。
C++版本
这里表示26个指针有两种方法
一个拥有26个指针的节点。(26个指针的链表)

Node *next[26];\\然后再初始化方法里赋值
    Node(){
        memset(next, 0, sizeof(next));
        isend=false;
    }

或者

vector<Node*> next =vector<Node*>(26,NULL);//struct里vector成员变量必须初始化完成,不然编译器可能无法区分这是一个成员函数声明还是一个成员变量声明,也就是产生歧义。
//vector<Node*> next
//vector<Node*> next(26,NULL) ↑←  都不可

#include <iostream>
#include <string.h>
#include <vector>
#include <string>
using namespace std;

struct Node{
    //https://blog.csdn.net/qq_32523711/article/details/107798999
    vector<Node*> next =vector<Node*>(26,NULL);//vector成员变量必须在这里初始化完毕    
    // Node *next[26];
    bool isend;
    Node(){
        // memset(next, 0, sizeof(next));
        isend=false;
    }
};
class Trie {
public:
    /** Initialize your data structure here. */
    Node *node;
    Trie() {
        node = new Node();
    }
    
    /** Inserts a word into the trie. */
    void insert(string word) {
               if(word.length()==0)
            return;
        Node *cur = this->node;
        for (int i = 0; i < word.length();i++){
            while(cur->next[word[i]-'a']==nullptr){
                cur->next[word[i] - 'a'] = new Node();
            }
            // if(i==word.length()-1){
            //     cur.isend=true;
            // }
            cur = cur->next[word[i] - 'a'];

        }
        cur->isend=true;
        
    }

    // }
    
    /** Returns if the word is in the trie. */
    bool search(string word) {
        Node *cur = this->node;
        for(int i=0;i<word.length();i++){
            if(cur->next[word[i]-'a']==nullptr)
                return false;
            cur = cur->next[word[i] - 'a'];
        }
        return cur->isend;

    }
    
    /** Returns if there is any word in the trie that starts with the given prefix. */
    bool startsWith(string prefix) {
        Node *cur = this->node;
        for(int i=0;i<prefix.length();i++){
            if(cur->next[prefix[i]-'a']==nullptr)
                return false;
            cur = cur->next[prefix[i] - 'a'];
        }
        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);
 */

相关文章

  • 前缀树 LC208

    前缀树就是一个拥有26个指针的链表打算实现C++和python 版本。C++版本这里表示26个指针有两种方法一个拥...

  • 前缀树(字典树/Trie)Java实现和应用

    摘要: 前缀树,字典树,插入查询逻辑,Java实现,时间复杂度分析 前缀树介绍 Trie树又被称为前缀树、字典树,...

  • 数据结构基础--前缀树&&后缀树

    本文只是自己的笔记,并不具备过多的指导意义。 前缀树 何为前缀树 前缀树又名字典树,单词查找树,Trie树,是一种...

  • 前缀树

    前缀树又名Tries树、字典树、单词查找树等,常用于快速检索,大量字符串的排序和统计等。 三个基本性质 根节点不包...

  • 前缀树

    前缀树原理

  • 前缀树

    概念: 简述:又名单词查找树,tries树,一种多路树形结构,常用来操作字符串(但不限于字符串),和hash效率有...

  • 前缀树

    题目 给定一个字符串数组,其中不含有重复字符串,判断是否有字符串是另一个字符串的前缀 思路 实现前缀树即可,判断是...

  • 前缀树

    1.什么是前缀树 前缀树是N叉树的一种特殊形式。通常来说,一个前缀树是用来存储字符串的。前缀树的每一个节点代表一个...

  • 前缀树

    最近看代码,发现了一个敏感词检测是用前缀树写的,看起来速度蛮快,毕竟是拿空间换时间,LOG倍速。但是缺点也很明显,...

  • 前缀树

网友评论

      本文标题:前缀树 LC208

      本文链接:https://www.haomeiwen.com/subject/ucpplltx.html