列表

详情


剑指 Offer II 066. 单词之和

实现一个 MapSum 类,支持两个方法,insert 和 sum

 

示例:

输入:
inputs = ["MapSum", "insert", "sum", "insert", "sum"]
inputs = [[], ["apple", 3], ["ap"], ["app", 2], ["ap"]]
输出:
[null, null, 3, null, 5]

解释:
MapSum mapSum = new MapSum();
mapSum.insert("apple", 3);  
mapSum.sum("ap");           // return 3 (apple = 3)
mapSum.insert("app", 2);    
mapSum.sum("ap");           // return 5 (apple + app = 3 + 2 = 5)

 

提示:

 

注意:本题与主站 677 题相同: https://leetcode.cn/problems/map-sum-pairs/

原站题解

去查看

上次编辑到这里,代码来自缓存 点击恢复默认模板
class MapSum { public: /** Initialize your data structure here. */ MapSum() { } void insert(string key, int val) { } int sum(string prefix) { } }; /** * Your MapSum object will be instantiated and called as such: * MapSum* obj = new MapSum(); * obj->insert(key,val); * int param_2 = obj->sum(prefix); */

python3 解法, 执行用时: 40 ms, 内存消耗: 15.1 MB, 提交时间: 2022-11-17 16:30:16

class MapSum:
    def __init__(self):
        self.map = {}
        self.prefixmap = {}

    def insert(self, key: str, val: int) -> None:
        delta = val
        if key in self.map:
            delta -= self.map[key]
        self.map[key] = val
        for i in range(len(key)):
            currprefix = key[0:i+1]
            if currprefix in self.prefixmap:
                self.prefixmap[currprefix] += delta
            else:
                self.prefixmap[currprefix] = delta

    def sum(self, prefix: str) -> int:
        if prefix in self.prefixmap:
            return self.prefixmap[prefix]
        else:
            return 0



# Your MapSum object will be instantiated and called as such:
# obj = MapSum()
# obj.insert(key,val)
# param_2 = obj.sum(prefix)

golang 解法, 执行用时: 0 ms, 内存消耗: 2.6 MB, 提交时间: 2022-11-17 16:29:51

// 前缀哈希映射
type MapSum struct {
    m, pre map[string]int
}

func Constructor() MapSum {
    return MapSum{map[string]int{}, map[string]int{}}
}

func (m *MapSum) Insert(key string, val int) {
    delta := val
    if m.m[key] > 0 {
        delta -= m.m[key]
    }
    m.m[key] = val
    for i := range key {
        m.pre[key[:i+1]] += delta
    }
}

func (m *MapSum) Sum(prefix string) int {
    return m.pre[prefix]
}

/**
 * Your MapSum object will be instantiated and called as such:
 * obj := Constructor();
 * obj.Insert(key,val);
 * param_2 := obj.Sum(prefix);
 */

golang 解法, 执行用时: 4 ms, 内存消耗: 2.4 MB, 提交时间: 2022-11-17 16:29:06

type MapSum map[string]int

func Constructor() MapSum {
    return MapSum{}
}

func (m MapSum) Insert(key string, val int) {
    m[key] = val
}

func (m MapSum) Sum(prefix string) (sum int) {
    for s, v := range m {
        if strings.HasPrefix(s, prefix) {
            sum += v
        }
    }
    return
}


/**
 * Your MapSum object will be instantiated and called as such:
 * obj := Constructor();
 * obj.Insert(key,val);
 * param_2 := obj.Sum(prefix);
 */

python3 解法, 执行用时: 40 ms, 内存消耗: 15.1 MB, 提交时间: 2022-11-17 16:28:48

'''
暴力扫描
'''
class MapSum:
    def __init__(self):
        self.map = {}

    def insert(self, key: str, val: int) -> None:
        self.map[key] = val

    def sum(self, prefix: str) -> int:
        res = 0
        for key,val in self.map.items():
            if key.startswith(prefix):
                res += val
        return res


# Your MapSum object will be instantiated and called as such:
# obj = MapSum()
# obj.insert(key,val)
# param_2 = obj.sum(prefix)

上一题