677. 键值映射
codeflysafe Lv5

实现一个 MapSum 类里的两个方法,insert 和 sum。

对于方法 insert,你将得到一对(字符串,整数)的键值对。字符串表示键,整数表示值。如果键已经存在,那么原来的键值对将被替代成新的键值对。

对于方法 sum,你将得到一个表示前缀的字符串,你需要返回所有以该前缀开头的键的值的总和。

示例 1:

1
2
3
4
输入: insert("apple", 3), 输出: Null
输入: sum("ap"), 输出: 3
输入: insert("app", 2), 输出: Null
输入: sum("ap"), 输出: 5

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/map-sum-pairs
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

Solution

字典树结构

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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
//字典树节点
class Node{

public:
char data;
int val;
bool leaf;
map<char,Node> next;

Node(char c){
data = c;
leaf = false;
}

Node(){};

};

class MapSum {
public:

Node root;

/** Initialize your data structure here. */
MapSum() {
root = Node();
}

void insert(string key, int val) {
Node *trie = &root;
int i =0,n = key.size();
char c;
while(i < n){
c = key[i];
if(!trie->next.count(c)){
trie->next[c] = Node(c);
}
trie = &trie->next[c];
i++;
}
trie->val = val;
trie->leaf = true;
}

// 递归求和
int trave(Node *trie){
int ans = 0;
if(trie->leaf) {ans += trie->val;}
for(auto &p:trie->next){
Node *t = &(p.second);
ans += trave(t);
}
return ans;
}

int sum(string prefix) {
Node *trie = &root;
int ans = 0;
int i =0, n = prefix.size();
char c;
while( i < n){
c = prefix[i];
if(!trie->next.count(c)) return 0;
trie = &trie->next[c];
i++;
}
return trave(trie);

}
};

/**
* Your MapSum object will be instantiated and called as such:
* MapSum* obj = new MapSum();
* obj->insert(key,val);
* int param_2 = obj->sum(prefix);
*/
  • 本文标题:677. 键值映射
  • 本文作者:codeflysafe
  • 创建时间:2020-05-07 10:10:35
  • 本文链接:https://codeflysafe.github.io/2020/05/07/2020-05-07-677.-键值映射/
  • 版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
 评论