Day 74 677. 键值映射

677. 键值映射

题目

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
实现一个 MapSum 类,支持两个方法,insert 和 sum:

MapSum() 初始化 MapSum 对象

void insert(String key, int val) 插入 key-val 键值对,字符串表示键 key ,整数表示值 val 。

如果键 key 已经存在,那么原来的键值对将被替代成新的键值对。

int sum(string prefix) 返回所有以该前缀 prefix 开头的键 key 的值的总和。
 

示例:

输入:
["MapSum", "insert", "sum", "insert", "sum"]
[[], ["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)
 

提示:

1 <= key.length, prefix.length <= 50
key 和 prefix 仅由小写英文字母组成
1 <= val <= 1000
最多调用 50 次 insert 和 sum

题目思路

  • 1、最简单方法直接使用cpp的四大容器组件即可中,但是由于是学习trie树的使用,所以使用tire来构建
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
const int N = 3000;
int ans[N][26];
int cnt[N];
int idx = 0;

class MapSum {
public:
MapSum() {
memset(ans, 0, sizeof ans); // 也可以使用动态数组进行i
memset(cnt, 0, sizeof cnt);
}

void insert(string key, int val) {
int p = 0;
for (auto &i : key)
{
int q = i - 'a';
if (!ans[p][q]) ans[p][q] = ++idx;
p = ans[p][q];
}
cnt[p] = val;
}

int sum(string prefix) {
int p = 0;
for (auto &i : prefix)
{
int q = i - 'a';
if (!ans[p][q]) return 0;
p = ans[p][q];
}
return dfs(p);
}

int dfs(int p) {
if (!p) return 0;
int res = cnt[p];
for (int i = 0; i < 26; ++i) {
res += dfs(ans[p][i]);
}
return res;
}
};

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

复杂度

  • 时间复杂度:O(n)
  • 空间复杂度:O(n * m * C)

Day 74 677. 键值映射
https://chaggle.github.io/2021/11/22/Leetcode/91-day/day-74/
作者
chaggle
发布于
2021年11月22日
许可协议