Skip to content

Commit 91190d3

Browse files
committed
feat: add solutions to lc/lcof2 problems: Map Sum Pairs
1 parent f2eecdc commit 91190d3

File tree

11 files changed

+608
-2
lines changed

11 files changed

+608
-2
lines changed

lcof2/剑指 Offer II 066. 单词之和/README.md

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,22 +51,148 @@ mapSum.sum(&quot;ap&quot;); // return 5 (<u>ap</u>ple + <u>ap</u>p = 3
5151

5252
<!-- 这里可写通用的实现逻辑 -->
5353

54+
利用哈希表存储每个键的所有前缀子串。
55+
5456
<!-- tabs:start -->
5557

5658
### **Python3**
5759

5860
<!-- 这里可写当前语言的特殊实现逻辑 -->
5961

6062
```python
63+
class MapSum:
64+
65+
def __init__(self):
66+
"""
67+
Initialize your data structure here.
68+
"""
69+
self.data = collections.defaultdict(int)
70+
self.t = collections.defaultdict(int)
71+
72+
def insert(self, key: str, val: int) -> None:
73+
old = self.t[key]
74+
self.t[key] = val
75+
for i in range(1, len(key) + 1):
76+
self.data[key[:i]] += val - old
77+
78+
def sum(self, prefix: str) -> int:
79+
return self.data[prefix]
6180

81+
82+
# Your MapSum object will be instantiated and called as such:
83+
# obj = MapSum()
84+
# obj.insert(key,val)
85+
# param_2 = obj.sum(prefix)
6286
```
6387

6488
### **Java**
6589

6690
<!-- 这里可写当前语言的特殊实现逻辑 -->
6791

6892
```java
93+
class MapSum {
94+
private Map<String, Integer> data;
95+
private Map<String, Integer> t;
96+
97+
/** Initialize your data structure here. */
98+
public MapSum() {
99+
data = new HashMap<>();
100+
t = new HashMap<>();
101+
}
102+
103+
public void insert(String key, int val) {
104+
int old = t.getOrDefault(key, 0);
105+
t.put(key, val);
106+
for (int i = 1; i < key.length() + 1; ++i) {
107+
String k = key.substring(0, i);
108+
data.put(k, data.getOrDefault(k, 0) + (val - old));
109+
}
110+
}
111+
112+
public int sum(String prefix) {
113+
return data.getOrDefault(prefix, 0);
114+
}
115+
}
116+
117+
/**
118+
* Your MapSum object will be instantiated and called as such:
119+
* MapSum obj = new MapSum();
120+
* obj.insert(key,val);
121+
* int param_2 = obj.sum(prefix);
122+
*/
123+
```
124+
125+
### **C++**
126+
127+
```cpp
128+
class MapSum {
129+
public:
130+
unordered_map<string, int> data;
131+
unordered_map<string, int> t;
132+
133+
/** Initialize your data structure here. */
134+
MapSum() {
135+
136+
}
137+
138+
void insert(string key, int val) {
139+
int old = t[key];
140+
t[key] = val;
141+
for (int i = 1; i < key.size() + 1; ++i)
142+
{
143+
string k = key.substr(0, i);
144+
data[k] += (val - old);
145+
}
146+
}
147+
148+
int sum(string prefix) {
149+
return data[prefix];
150+
}
151+
};
152+
153+
/**
154+
* Your MapSum object will be instantiated and called as such:
155+
* MapSum* obj = new MapSum();
156+
* obj->insert(key,val);
157+
* int param_2 = obj->sum(prefix);
158+
*/
159+
```
69160
161+
### **Go**
162+
163+
```go
164+
type MapSum struct {
165+
data map[string]int
166+
t map[string]int
167+
}
168+
169+
/** Initialize your data structure here. */
170+
func Constructor() MapSum {
171+
return MapSum{
172+
data: make(map[string]int),
173+
t: make(map[string]int),
174+
}
175+
}
176+
177+
func (this *MapSum) Insert(key string, val int) {
178+
old := this.t[key]
179+
this.t[key] = val
180+
for i := 1; i < len(key)+1; i++ {
181+
k := key[:i]
182+
this.data[k] += (val - old)
183+
}
184+
}
185+
186+
func (this *MapSum) Sum(prefix string) int {
187+
return this.data[prefix]
188+
}
189+
190+
/**
191+
* Your MapSum object will be instantiated and called as such:
192+
* obj := Constructor();
193+
* obj.Insert(key,val);
194+
* param_2 := obj.Sum(prefix);
195+
*/
70196
```
71197

72198
### **...**
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class MapSum {
2+
public:
3+
unordered_map<string, int> data;
4+
unordered_map<string, int> t;
5+
6+
/** Initialize your data structure here. */
7+
MapSum() {
8+
9+
}
10+
11+
void insert(string key, int val) {
12+
int old = t[key];
13+
t[key] = val;
14+
for (int i = 1; i < key.size() + 1; ++i)
15+
{
16+
string k = key.substr(0, i);
17+
data[k] += (val - old);
18+
}
19+
}
20+
21+
int sum(string prefix) {
22+
return data[prefix];
23+
}
24+
};
25+
26+
/**
27+
* Your MapSum object will be instantiated and called as such:
28+
* MapSum* obj = new MapSum();
29+
* obj->insert(key,val);
30+
* int param_2 = obj->sum(prefix);
31+
*/
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
type MapSum struct {
2+
data map[string]int
3+
t map[string]int
4+
}
5+
6+
/** Initialize your data structure here. */
7+
func Constructor() MapSum {
8+
return MapSum{
9+
data: make(map[string]int),
10+
t: make(map[string]int),
11+
}
12+
}
13+
14+
func (this *MapSum) Insert(key string, val int) {
15+
old := this.t[key]
16+
this.t[key] = val
17+
for i := 1; i < len(key)+1; i++ {
18+
k := key[:i]
19+
this.data[k] += (val - old)
20+
}
21+
}
22+
23+
func (this *MapSum) Sum(prefix string) int {
24+
return this.data[prefix]
25+
}
26+
27+
/**
28+
* Your MapSum object will be instantiated and called as such:
29+
* obj := Constructor();
30+
* obj.Insert(key,val);
31+
* param_2 := obj.Sum(prefix);
32+
*/
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class MapSum {
2+
private Map<String, Integer> data;
3+
private Map<String, Integer> t;
4+
5+
/** Initialize your data structure here. */
6+
public MapSum() {
7+
data = new HashMap<>();
8+
t = new HashMap<>();
9+
}
10+
11+
public void insert(String key, int val) {
12+
int old = t.getOrDefault(key, 0);
13+
t.put(key, val);
14+
for (int i = 1; i < key.length() + 1; ++i) {
15+
String k = key.substring(0, i);
16+
data.put(k, data.getOrDefault(k, 0) + (val - old));
17+
}
18+
}
19+
20+
public int sum(String prefix) {
21+
return data.getOrDefault(prefix, 0);
22+
}
23+
}
24+
25+
/**
26+
* Your MapSum object will be instantiated and called as such:
27+
* MapSum obj = new MapSum();
28+
* obj.insert(key,val);
29+
* int param_2 = obj.sum(prefix);
30+
*/
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class MapSum:
2+
3+
def __init__(self):
4+
"""
5+
Initialize your data structure here.
6+
"""
7+
self.data = collections.defaultdict(int)
8+
self.t = collections.defaultdict(int)
9+
10+
def insert(self, key: str, val: int) -> None:
11+
old = self.t[key]
12+
self.t[key] = val
13+
for i in range(1, len(key) + 1):
14+
self.data[key[:i]] += val - old
15+
16+
def sum(self, prefix: str) -> int:
17+
return self.data[prefix]
18+
19+
20+
# Your MapSum object will be instantiated and called as such:
21+
# obj = MapSum()
22+
# obj.insert(key,val)
23+
# param_2 = obj.sum(prefix)

0 commit comments

Comments
 (0)