LeetCode(677):鍵值映射 Map Sum Pairs(Java)

2019.11.18 LeetCode 從零單刷個人筆記整理(持續更新)

github:https://github.com/ChopinXBP/LeetCode-Babel

這題需要用道前綴樹+哈希表,前綴樹每個結點存放以其爲前綴的鍵值總和,哈希表每個結點存放插入的元素及其最新插入值。

插入

插入可能有兩種情況:

1.key首次插入,map中不存在,前綴路徑的所有結點value更新爲val值

2.key曾插入num值,並保存在map中,val將替換num(差值delta=val-num),前綴路徑所有結點value對delta更新(value+=delta)

插入結束將新的key和val保存在map中。

讀取

讀取時,深度遍歷prefix的前綴路徑,如果出現null值則返回0,遍歷結束返回尾結點的value值。


傳送門:鍵值映射

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.

實現一個 MapSum 類裏的兩個方法,insert 和 sum。

對於方法 insert,你將得到一對(字符串,整數)的鍵值對。字符串表示鍵,整數表示值。如果鍵已經存在,那麼原來的鍵值對將被替代成新的鍵值對。

對於方法 sum,你將得到一個表示前綴的字符串,你需要返回所有以該前綴開頭的鍵的值的總和。

示例 1:
輸入: insert("apple", 3), 輸出: Null
輸入: sum("ap"), 輸出: 3
輸入: insert("app", 2), 輸出: Null
輸入: sum("ap"), 輸出: 5


import java.util.HashMap;

/**
 *
 * 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.
 *
 */

public class MapSumPairs {
    //前綴樹
    class TrieNode{
        HashMap<Character, TrieNode> children = new HashMap<>();
        int value;
    }

    class MapSum {

        private HashMap<String, Integer> map;
        private TrieNode root;

        //前綴樹每個結點存放以其爲前綴的鍵值總和,哈希表每個結點存放插入的元素及其最新插入值
        /** Initialize your data structure here. */
        public MapSum() {
            map = new HashMap<>();
            root = new TrieNode();
        }

        //插入可能有兩種情況:
        //1.key首次插入,map中不存在,前綴路徑的所有結點value更新爲val值
        //2.key曾插入num值,並保存在map中,val將替換num(差值delta=val-num),前綴路徑所有結點value對delta更新(value+=delta)
        //最後將新的key和val保存在map中
        public void insert(String key, int val) {
            int delta = val - map.getOrDefault(key, 0);
            TrieNode node = root;
            node.value += delta;
            for(char c : key.toCharArray()){
                node.children.putIfAbsent(c, new TrieNode());
                node = node.children.get(c);
                node.value += delta;
            }
            map.put(key, val);
        }

        //深度遍歷prefix的前綴路徑,如果出現null值則返回0,遍歷結束返回尾結點的value值
        public int sum(String prefix) {
            TrieNode node = root;
            for(char c : prefix.toCharArray()){
                node = node.children.get(c);
                if(node == null){
                    return 0;
                }
            }
            return node.value;
        }
    }

/**
 * Your MapSum object will be instantiated and called as such:
 * MapSum obj = new MapSum();
 * obj.insert(key,val);
 * int param_2 = obj.sum(prefix);
 */
}



#Coding一小時,Copying一秒鐘。留個言點個讚唄,謝謝你#

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章