mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-09-05 07:21:40 +08:00
update
This commit is contained in:
42
leetcode/problem/map-sum-pairs.html
Normal file
42
leetcode/problem/map-sum-pairs.html
Normal file
@@ -0,0 +1,42 @@
|
||||
<p>Design a map that allows you to do the following:</p>
|
||||
|
||||
<ul>
|
||||
<li>Maps a string key to a given value.</li>
|
||||
<li>Returns the sum of the values that have a key with a prefix equal to a given string.</li>
|
||||
</ul>
|
||||
|
||||
<p>Implement the <code>MapSum</code> class:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>MapSum()</code> Initializes the <code>MapSum</code> object.</li>
|
||||
<li><code>void insert(String key, int val)</code> Inserts the <code>key-val</code> pair into the map. If the <code>key</code> already existed, the original <code>key-value</code> pair will be overridden to the new one.</li>
|
||||
<li><code>int sum(string prefix)</code> Returns the sum of all the pairs' value whose <code>key</code> starts with the <code>prefix</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input</strong>
|
||||
["MapSum", "insert", "sum", "insert", "sum"]
|
||||
[[], ["apple", 3], ["ap"], ["app", 2], ["ap"]]
|
||||
<strong>Output</strong>
|
||||
[null, null, 3, null, 5]
|
||||
|
||||
<strong>Explanation</strong>
|
||||
MapSum mapSum = new MapSum();
|
||||
mapSum.insert("apple", 3);
|
||||
mapSum.sum("ap"); // return 3 (<u>ap</u>ple = 3)
|
||||
mapSum.insert("app", 2);
|
||||
mapSum.sum("ap"); // return 5 (<u>ap</u>ple + <u>ap</u>p = 3 + 2 = 5)
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= key.length, prefix.length <= 50</code></li>
|
||||
<li><code>key</code> and <code>prefix</code> consist of only lowercase English letters.</li>
|
||||
<li><code>1 <= val <= 1000</code></li>
|
||||
<li>At most <code>50</code> calls will be made to <code>insert</code> and <code>sum</code>.</li>
|
||||
</ul>
|
Reference in New Issue
Block a user