力扣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);
 */
相关推荐
摇滚侠2 小时前
面试实战 问题二十四 Spring 框架中循环依赖问题的解决方法
java·后端·spring
源代码•宸2 小时前
MySQL 索引:索引为什么使用 B+树?(详解B树、B+树)
数据结构·数据库·经验分享·b树·mysql·b+树·b-树
三木水4 小时前
Spring-rabbit使用实战七
java·分布式·后端·spring·消息队列·java-rabbitmq·java-activemq
别来无恙1494 小时前
Spring Boot文件下载功能实现详解
java·spring boot·后端·数据导出
optimistic_chen4 小时前
【Java EE初阶 --- 网络原理】JVM
java·jvm·笔记·网络协议·java-ee
ccut 第一混4 小时前
c#联合Halcon进行OCR字符识别(含halcon-25.05 百度网盘)
c#·ocr·halcon
weixin_456904274 小时前
Java泛型与委托
java·spring boot·spring
悟能不能悟5 小时前
能刷java题的网站
java·开发语言
程序员陆通6 小时前
Java高并发场景下的缓存穿透问题定位与解决方案
java·开发语言·缓存
北执南念6 小时前
Java多线程基础总结
java