• 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  */
  • 相关阅读:
    组件开发的博客信息
    读书片断之 开发自定义HTTP模块
    .NET2.0抓取网页全部链接
    数据分页处理方法汇总(例子)
    GridView添加统计(合计)行
    JS中$含义及用法
    Hashtable, ArrayList, List, Dictionary学习
    C#自定义消息映射!
    winform等待窗口
    DataView的RowFilter特殊字符的处理
  • 原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/11848195.html
Copyright © 2020-2023  润新知