力扣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);
 */
相关推荐
2501_947575801 小时前
计算机毕业设计之jsp开山车行二手车交易系统
java·开发语言·hadoop·python·信息可视化·django·课程设计
骑士雄师1 小时前
java面试题 4:鉴权
java·开发语言
古城小栈2 小时前
为啥说:训练用BF16,推理用FP16
人工智能·算法·机器学习
KaMeidebaby2 小时前
卡梅德生物技术快报|蛋白 N 端测序在重组贻贝融合蛋白表征中的应用,解决原核表达序列偏移工艺难题
前端·人工智能·物联网·算法·百度
帅次3 小时前
Android 高级工程师面试:Java 基础知识 近1年高频追问 22 题
android·java·面试
蓝胖的四次元口袋3 小时前
Java集合(4)
java·哈希算法
Turbo正则3 小时前
群论在AI中的应用概述
人工智能·算法·抽象代数
ysa0510303 小时前
【并查集】判环
c++·笔记·算法
2501_948106913 小时前
计算机毕业设计之基于jsp教科研信息共享系统
java·开发语言·信息可视化·spark·课程设计
TanYYF3 小时前
spring ai入门教程二
java·人工智能·spring