力扣labuladong——一刷day92

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


前言


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

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

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. 键值映射

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);
 */

相关推荐

  1. labuladong——day92

    2024-01-17 10:34:06       48 阅读
  2. labuladong——day90

    2024-01-17 10:34:06       54 阅读
  3. labuladong——day91

    2024-01-17 10:34:06       66 阅读
  4. labuladong——day96

    2024-01-17 10:34:06       58 阅读
  5. labuladong——day95

    2024-01-17 10:34:06       53 阅读
  6. labuladong——day68

    2024-01-17 10:34:06       57 阅读
  7. labuladong——day67

    2024-01-17 10:34:06       60 阅读
  8. labuladong——day69

    2024-01-17 10:34:06       58 阅读
  9. labuladong——day66

    2024-01-17 10:34:06       52 阅读
  10. labuladong——day70

    2024-01-17 10:34:06       63 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-01-17 10:34:06       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-17 10:34:06       106 阅读
  3. 在Django里面运行非项目文件

    2024-01-17 10:34:06       87 阅读
  4. Python语言-面向对象

    2024-01-17 10:34:06       96 阅读

热门阅读

  1. 开发安全之:Cross-Site Scripting (XSS) 漏洞

    2024-01-17 10:34:06       49 阅读
  2. python实现屏幕颜色获取

    2024-01-17 10:34:06       60 阅读
  3. Jupyter Notebook之移除anaconda环境

    2024-01-17 10:34:06       55 阅读
  4. 济南大学 《计算机网络2a》期末考点

    2024-01-17 10:34:06       45 阅读
  5. spring.cloud.sentinel.eager=true这个有什么作用

    2024-01-17 10:34:06       49 阅读
  6. axios query传数组参数的格式

    2024-01-17 10:34:06       50 阅读
  7. Python 3 字符串的基本使用

    2024-01-17 10:34:06       44 阅读
  8. ChatGPT 和文心一言哪个更好用?

    2024-01-17 10:34:06       52 阅读
  9. GitHub 异常 - 无法连接22端口 Connection timed out

    2024-01-17 10:34:06       41 阅读
  10. Python爬虫---scrapy框架---下载嵌套数据

    2024-01-17 10:34:06       42 阅读