剑指 Offer II 066:单词之和

This commit is contained in:
轩辕龙儿 2022-05-27 16:20:39 +08:00
parent ef4025102f
commit 28b8aaae41
2 changed files with 130 additions and 0 deletions

View File

@ -0,0 +1,88 @@
//<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>
//<div><div>Related Topics</div><div><li>设计</li><li>字典树</li><li>哈希表</li><li>字符串</li></div></div><br><div><li>👍 16</li><li>👎 0</li></div>
package leetcode.editor.cn;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
// 剑指 Offer II 066:单词之和
public class Z1R5dt {
public static void main(String[] args) {
Solution solution = new Z1R5dt().new Solution();
// TO TEST
}
//leetcode submit region begin(Prohibit modification and deletion)
class MapSum {
Map<String, Integer> map;
public MapSum() {
map = new HashMap<>();
}
public void insert(String key, int val) {
map.put(key, val);
}
public int sum(String prefix) {
int res = 0;
for (String key : map.keySet()) {
if (key.startsWith(prefix)) {
res += map.get(key);
}
}
return res;
}
}
/**
* Your MapSum object will be instantiated and called as such:
* MapSum obj = new MapSum();
* obj.insert(key,val);
* int param_2 = obj.sum(prefix);
*/
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,42 @@
<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>
<div><div>Related Topics</div><div><li>设计</li><li>字典树</li><li>哈希表</li><li>字符串</li></div></div><br><div><li>👍 16</li><li>👎 0</li></div>