1
0
mirror of https://gitee.com/coder-xiaomo/leetcode-problemset synced 2025-01-11 02:58:13 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee
leetcode-problemset/leetcode-cn/problem (Chinese)/单词之和 [z1R5dt].html
2022-03-29 12:43:11 +08:00

42 lines
1.8 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<p>实现一个 <code>MapSum</code> 类,支持两个方法,<code>insert</code>&nbsp;&nbsp;<code>sum</code></p>
<ul>
<li><code>MapSum()</code> 初始化 <code>MapSum</code> 对象</li>
<li><code>void insert(String key, int val)</code> 插入 <code>key-val</code> 键值对,字符串表示键 <code>key</code> ,整数表示值 <code>val</code> 。如果键 <code>key</code> 已经存在,那么原来的键值对将被替代成新的键值对。</li>
<li><code>int sum(string prefix)</code> 返回所有以该前缀 <code>prefix</code> 开头的键 <code>key</code> 的值的总和。</li>
</ul>
<p>&nbsp;</p>
<p><strong>示例:</strong></p>
<pre>
<strong>输入:</strong>
inputs = [&quot;MapSum&quot;, &quot;insert&quot;, &quot;sum&quot;, &quot;insert&quot;, &quot;sum&quot;]
inputs = [[], [&quot;apple&quot;, 3], [&quot;ap&quot;], [&quot;app&quot;, 2], [&quot;ap&quot;]]
<strong>输出:</strong>
[null, null, 3, null, 5]
<strong>解释:</strong>
MapSum mapSum = new MapSum();
mapSum.insert(&quot;apple&quot;, 3);
mapSum.sum(&quot;ap&quot;); // return 3 (<u>ap</u>ple = 3)
mapSum.insert(&quot;app&quot;, 2);
mapSum.sum(&quot;ap&quot;); // return 5 (<u>ap</u>ple + <u>ap</u>p = 3 + 2 = 5)
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 &lt;= key.length, prefix.length &lt;= 50</code></li>
<li><code>key</code><code>prefix</code> 仅由小写英文字母组成</li>
<li><code>1 &lt;= val &lt;= 1000</code></li>
<li>最多调用 <code>50</code><code>insert</code><code>sum</code></li>
</ul>
<p>&nbsp;</p>
<p><meta charset="UTF-8" />注意:本题与主站 677&nbsp;题相同:&nbsp;<a href="https://leetcode-cn.com/problems/map-sum-pairs/">https://leetcode-cn.com/problems/map-sum-pairs/</a></p>