zoukankan      html  css  js  c++  java
  • LeetCode 677. Map Sum Pairs

    原题链接在这里:https://leetcode.com/problems/map-sum-pairs/

    题目:

    Implement a MapSum class with insert, and sum methods.

    For the method insert, you'll be given a pair of (string, integer). The string represents the key and the integer represents the value. If the key already existed, then the original key-value pair will be overridden to the new one.

    For the method sum, you'll be given a string representing the prefix, and you need to return the sum of all the pairs' value whose key starts with the prefix.

    Example 1:

    Input: insert("apple", 3), Output: Null
    Input: sum("ap"), Output: 3
    Input: insert("app", 2), Output: Null
    Input: sum("ap"), Output: 5

    题解:

    When inserting the key, construct Trie. TrieNode has int value. 

    Everytime when iterating this node again with new word, add new value to this node value.

    If the word is already inserted before, then add the delta to node value.

    When getting sum, iterate prefix's each char to the TrieNode and return that node's value.

    Time Complexity: insert, O(key.length()). sum, O(prefix.length()).

    Space: O(nm). n is count of key. m is average length of key.

    AC Java: 

     1 class MapSum {
     2     TrieNode root;
     3     HashMap<String, Integer> hm;
     4     
     5     /** Initialize your data structure here. */
     6     public MapSum() {
     7         root = new TrieNode(0); 
     8         hm = new HashMap<>();
     9     }
    10     
    11     public void insert(String key, int val) {
    12         int temp = val;
    13         if(hm.containsKey(key)){
    14             val = val - hm.get(key);
    15         }
    16         
    17         hm.put(key, temp);
    18         
    19         TrieNode p = root;
    20         for(char c : key.toCharArray()){
    21             if(p.nexts[c-'a'] == null){
    22                 p.nexts[c-'a'] = new TrieNode(val);
    23             }else{
    24                 p.nexts[c-'a'].val += val;
    25             }
    26             
    27             p = p.nexts[c-'a'];
    28         }
    29     }
    30     
    31     public int sum(String prefix) {
    32         TrieNode p = root;
    33         for(char c : prefix.toCharArray()){
    34             if(p.nexts[c-'a'] == null){
    35                 return 0;
    36             }
    37             
    38             p = p.nexts[c-'a'];
    39         }
    40         
    41         return p.val;
    42     }
    43 }
    44 
    45 class TrieNode{
    46     int val;
    47     TrieNode [] nexts;
    48     
    49     public TrieNode(int val){
    50         this.val = val;
    51         nexts = new TrieNode[26];
    52     }
    53 }
    54 
    55 /**
    56  * Your MapSum object will be instantiated and called as such:
    57  * MapSum obj = new MapSum();
    58  * obj.insert(key,val);
    59  * int param_2 = obj.sum(prefix);
    60  */
  • 相关阅读:
    Android组件化框架设计与实践
    浅谈Android进阶之路
    Android APP 性能优化的一些思考
    有关 Hybrid 开发模式实践总结
    开发人员必备的技能——单元测试
    有关Android插件化思考
    人生的意义到底是什么?
    Asp.Net Core 3.1学习-读取、监听json配置文件(7)
    Asp.Net Core 3.1学习-依赖注入、服务生命周期(6)
    Asp.Net Core 3.1学习- 应用程序的启动过程(5)
  • 原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/11848195.html
Copyright © 2011-2022 走看看