力扣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);
 */
相关推荐
IronMurphy5 小时前
【算法四十三】279. 完全平方数
算法
lee_curry5 小时前
第四章 jvm中的垃圾回收器
java·jvm·垃圾收集器
墨染天姬6 小时前
【AI】Hermes的GEPA算法
人工智能·算法
papership6 小时前
【入门级-数据结构-3、特殊树:完全二叉树的数组表示法】
数据结构·算法·链表
weixin_520649876 小时前
WinForm数据展示组件ListView
c#
smj2302_796826526 小时前
解决leetcode第3911题.移除子数组元素后第k小偶数
数据结构·python·算法·leetcode
山甫aa6 小时前
差分数组 ----- 从零开始的数据结构
数据结构
早日退休!!!6 小时前
《数据结构选型指南》笔记
数据结构·数据库·oracle
九转成圣7 小时前
Java 性能优化实战:如何将海量扁平数据高效转化为类目字典树?
java·开发语言·json
Beginner x_u7 小时前
链表专题:JS 实现原理与高频算法题总结
javascript·算法·链表