-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMap_Sum_Pairs.java
57 lines (46 loc) · 1.22 KB
/
Map_Sum_Pairs.java
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
// https://leetcode.com/problems/map-sum-pairs/
class MapSum {
class TrieNode {
private String word;
private int val;
private TrieNode[] children;
}
TrieNode root;
/** Initialize your data structure here. */
public MapSum() {
root = new TrieNode();
}
public void insert(String key, int val) {
TrieNode current = root;
for (int i = 0; i < key.length(); i++) {
if(current.children == null) current.children = new TrieNode[26];
int index = (int)(key.charAt(i)-'a');
if(current.children[index] == null) current.children[index] = new TrieNode();
current = current.children[index];
}
current.word = key;
current.val = val;
}
public int sum(String prefix) {
TrieNode current = root;
for (int i = 0; i < prefix.length() && current != null; i++) {
if(current.children == null) current.children = new TrieNode[26];
int index = (int)(prefix.charAt(i)-'a');
current = current.children[index];
}
return sum(current);
}
int sum(TrieNode root) {
int s = 0;
if (root == null || root.word == null) s = 0;
else s = root.val;
if (root != null && root.children != null) {
for (TrieNode n : root.children) {
s += sum(n);
}
}
return s;
}
}
public class Map_Sum_Pairs {
}