力扣labuladong——一刷day92

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档

文章目录

  • 前言
  • [一、力扣211. 添加与搜索单词 - 数据结构设计](#一、力扣211. 添加与搜索单词 - 数据结构设计)
  • [二、力扣677. 键值映射](#二、力扣677. 键值映射)

前言


Trie 树又叫字典树、前缀树、单词查找树,是一种二叉树衍生出来的高级数据结构,主要应用场景是处理字符串前缀相关的操作

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

java 复制代码
class WordDictionary {
    static final int R = 26;
    TrieNode root = null;
    static class TrieNode{
        String val;
        TrieNode[] children = new TrieNode[R];
    }

    public WordDictionary() {

    }
    
    public void addWord(String word) {
        root = put(root,word,0);
    }
    
    public boolean search(String word) {
        return get(root,word,0);
    }
    boolean get(TrieNode node, String word, int index){
        if(node == null){
            return false;
        }
        if(index == word.length()){
            if(node.val != null){
                return true;
            }
            return false;
        }
        char c = word.charAt(index);
        if(c == '.'){
            for(int i = 0; i < R; i ++){
                if(get(node.children[i],word,index+1)){
                    return true;
                }
            }
        }else{
            if(get(node.children[c-'a'],word,index+1)){
                return true;
            }
        }
        return false;
    }
    TrieNode put(TrieNode node, String word, int index){
        if(node == null){
            node = new TrieNode();
        }
        if(index == word.length()){
            node.val = word;
            return node;
        }
        char c = word.charAt(index);
        node.children[c-'a'] = put(node.children[c-'a'],word,index+1);
        return node;
    }
}

/**
 * Your WordDictionary object will be instantiated and called as such:
 * WordDictionary obj = new WordDictionary();
 * obj.addWord(word);
 * boolean param_2 = obj.search(word);
 */

二、力扣677. 键值映射

java 复制代码
class MapSum {
    static final int R = 26;
    TrieNode root = null;
    int sum = 0;
    static class TrieNode{
        int val = 0;
        TrieNode[] children = new TrieNode[R];
    }

    public MapSum() {

    }
    
    public void insert(String key, int val) {
        root = put(root,key,0,val);
    }
    
    public int sum(String prefix) {
        get(root,prefix,0);
        int temp = sum;
        sum = 0;
        return temp;
    }
    void get(TrieNode node, String prefix, int index){
        if(node == null){
            return;
        }
        if(index < prefix.length()){
            char c = prefix.charAt(index);
            // sum += node.val;
            get(node.children[c-'a'],prefix,index+1);
        }else{
            sum += node.val;
            for(int i = 0; i < R; i ++){
                
                if(node.children[i] != null){
                    get(node.children[i],prefix,index+1);
                }
            }
        }
    }
    TrieNode put(TrieNode node, String key, int index,int val){
        if(node == null){
            node = new TrieNode();
        }
        if(index == key.length()){
            node.val = val;
            return node;
        }
        char c = key.charAt(index);
        node.children[c-'a'] = put(node.children[c-'a'],key,index+1,val);
        return node;
    }
}

/**
 * Your MapSum object will be instantiated and called as such:
 * MapSum obj = new MapSum();
 * obj.insert(key,val);
 * int param_2 = obj.sum(prefix);
 */
相关推荐
董董灿是个攻城狮21 分钟前
AI视觉连载8:传统 CV 之边缘检测
算法
怒放吧德德2 小时前
Netty 4.2 入门指南:从概念到第一个程序
java·后端·netty
雨中飘荡的记忆4 小时前
大流量下库存扣减的数据库瓶颈:Redis分片缓存解决方案
java·redis·后端
心之语歌6 小时前
基于注解+拦截器的API动态路由实现方案
java·后端
华仔啊7 小时前
Stream 代码越写越难看?JDFrame 让 Java 逻辑回归优雅
java·后端
ray_liang7 小时前
用六边形架构与整洁架构对比是伪命题?
java·架构
AI软著研究员7 小时前
程序员必看:软著不是“面子工程”,是代码的“法律保险”
算法
FunnySaltyFish8 小时前
什么?Compose 把 GapBuffer 换成了 LinkBuffer?
算法·kotlin·android jetpack
Ray Liang9 小时前
用六边形架构与整洁架构对比是伪命题?
java·python·c#·架构设计
颜酱9 小时前
理解二叉树最近公共祖先(LCA):从基础到变种解析
javascript·后端·算法