代码拉取完成,页面将自动刷新
同步操作将从 小墨/力扣题库(完整版) 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
<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>
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。