力扣面试150题--实现Trie(前缀树)

Day 67

题目描述

思路

初次思路 :此时还不了解什么是前缀树,尝试自己实现一下

由于我们需要快速定位前缀和字符串,于是我想到了使用hashset实现,tes用于存放字符串,prefixs存放前缀,获取前缀通过使用substring进行拆分。

java 复制代码
class Trie {
    Set<String>tes;
    Set<String>prefixs;
    public Trie() {
        tes=new HashSet<String>();
        prefixs=new HashSet<String>();
        num=new ArrayList<String>();
    }
    
    public void insert(String word) {
        if(tes.contains(word)){
            return;
        }
        else{
            tes.add(word);
            for(int i=0;i<=word.length();i++){
                String a=word.substring(0,i);
                prefixs.add(a);
            }
        }
    }
    
    public boolean search(String word) {
        return tes.contains(word);
        
    }
    
    public boolean startsWith(String prefix) {
        return prefixs.contains(prefix);
    }
}

/**
 * Your Trie object will be instantiated and called as such:
 * Trie obj = new Trie();
 * obj.insert(word);
 * boolean param_2 = obj.search(word);
 * boolean param_3 = obj.startsWith(prefix);
 */

学习前缀树后

前缀树的作用在于快速检索字符串的前缀,插入一个字符串,即为从根一次插入孩子节点,将字符串最后一个字符对应的节点标记结束节点,再插入另外一个相同前缀但最后n个字符不一样的字符串,那么在相同前缀的部分,不需要插入新的节点,直到第一个不同的字符,添加一个新的子节点。

这样做的好处,我们如果要获取两个字符串的前缀,只需要从根节点向下遍历,比较两个字符串,只要到某个节点出现了分支,则这个节点之前的节点就是两个字符的公共前缀。同时节约了存储空间

做法如下:

java 复制代码
class Trie {
    public Trie[]child;//孩子节点,可能插入的是26个英文字母中的一个
    public boolean isend;//判断是否为一个字符串的结束 区分前缀和字符串
    public Trie() {
        child=new Trie[26];
        isend=false;
    }
    
    public void insert(String word) {
        Trie node=this;//根节点
        for(int i=0;i<word.length();i++){
            char a=word.charAt(i);
            int index=a-'a';//转化为序号
            if(node.child[index]==null){
                node.child[index]=new Trie();//创建为新孩子
            }
            node=node.child[index];//移动到下一个孩子
        }
        node.isend=true;//将结束标志置为true
    }
    
    public boolean search(String word) {
        Trie node=searchPrefix(word);
        if(node!=null&&node.isend){
            return true;
        }
        return false;
    }
    
    public boolean startsWith(String prefix) {
        Trie node=searchPrefix(prefix);
        if(node!=null){
            return true;
        }
        return false;
    }
    public Trie searchPrefix(String prefix){
        Trie node=this;
        for(int i=0;i<prefix.length();i++){
            char a=prefix.charAt(i);
            int index=a-'a';
            if(node.child[index]==null){//还没遍历完前缀就结束了 说明找不到
                return null;
            }
            node=node.child[index];
        }
        return node;
    }
}

/**
 * Your Trie object will be instantiated and called as such:
 * Trie obj = new Trie();
 * obj.insert(word);
 * boolean param_2 = obj.search(word);
 * boolean param_3 = obj.startsWith(prefix);
 */
相关推荐
啦啦啦_999915 小时前
Redis-5-doFormatAsync()方法
数据库·redis·c#
网络安全-杰克16 小时前
2026面试自动化测试面试题【含答案】
自动化测试·软件测试·面试·职场和发展
Porco.w16 小时前
C#与三菱PLC FX5U通信
网络·c#
努力学算法的蒟蒻17 小时前
day79(2.7)——leetcode面试经典150
算法·leetcode·职场和发展
2401_8414956417 小时前
【LeetCode刷题】二叉树的层序遍历
数据结构·python·算法·leetcode·二叉树··队列
2401_8414956418 小时前
【LeetCode刷题】二叉树的直径
数据结构·python·算法·leetcode·二叉树··递归
我是咸鱼不闲呀18 小时前
力扣Hot100系列19(Java)——[动态规划]总结(上)(爬楼梯,杨辉三角,打家劫舍,完全平方数,零钱兑换)
java·leetcode·动态规划
E_ICEBLUE18 小时前
PPT 批量转图片:在 Web 预览中实现翻页效果(C#/VB.NET)
c#·powerpoint·svg
铉铉这波能秀19 小时前
LeetCode Hot100数据结构背景知识之列表(List)Python2026新版
数据结构·leetcode·list
仟濹19 小时前
算法打卡 day1 (2026-02-06 周四) | 算法: DFS | 1_卡码网98 可达路径 | 2_力扣797_所有可能的路径
算法·leetcode·深度优先