剑指 Offer II 066:单词之和
This commit is contained in:
parent
ef4025102f
commit
28b8aaae41
88
src/main/java/leetcode/editor/cn/Z1R5dt.java
Normal file
88
src/main/java/leetcode/editor/cn/Z1R5dt.java
Normal file
@ -0,0 +1,88 @@
|
||||
//<p>实现一个 <code>MapSum</code> 类,支持两个方法,<code>insert</code> 和 <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> </p>
|
||||
//
|
||||
//<p><strong>示例:</strong></p>
|
||||
//
|
||||
//<pre>
|
||||
//<strong>输入:</strong>
|
||||
//inputs = ["MapSum", "insert", "sum", "insert", "sum"]
|
||||
//inputs = [[], ["apple", 3], ["ap"], ["app", 2], ["ap"]]
|
||||
//<strong>输出:</strong>
|
||||
//[null, null, 3, null, 5]
|
||||
//
|
||||
//<strong>解释:</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>提示:</strong></p>
|
||||
//
|
||||
//<ul>
|
||||
// <li><code>1 <= key.length, prefix.length <= 50</code></li>
|
||||
// <li><code>key</code> 和 <code>prefix</code> 仅由小写英文字母组成</li>
|
||||
// <li><code>1 <= val <= 1000</code></li>
|
||||
// <li>最多调用 <code>50</code> 次 <code>insert</code> 和 <code>sum</code></li>
|
||||
//</ul>
|
||||
//
|
||||
//<p> </p>
|
||||
//
|
||||
//<p><meta charset="UTF-8" />注意:本题与主站 677 题相同: <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)
|
||||
|
||||
}
|
42
src/main/java/leetcode/editor/cn/doc/content/Z1R5dt.md
Normal file
42
src/main/java/leetcode/editor/cn/doc/content/Z1R5dt.md
Normal file
@ -0,0 +1,42 @@
|
||||
<p>实现一个 <code>MapSum</code> 类,支持两个方法,<code>insert</code> 和 <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> </p>
|
||||
|
||||
<p><strong>示例:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>
|
||||
inputs = ["MapSum", "insert", "sum", "insert", "sum"]
|
||||
inputs = [[], ["apple", 3], ["ap"], ["app", 2], ["ap"]]
|
||||
<strong>输出:</strong>
|
||||
[null, null, 3, null, 5]
|
||||
|
||||
<strong>解释:</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>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= key.length, prefix.length <= 50</code></li>
|
||||
<li><code>key</code> 和 <code>prefix</code> 仅由小写英文字母组成</li>
|
||||
<li><code>1 <= val <= 1000</code></li>
|
||||
<li>最多调用 <code>50</code> 次 <code>insert</code> 和 <code>sum</code></li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><meta charset="UTF-8" />注意:本题与主站 677 题相同: <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>
|
Loading…
Reference in New Issue
Block a user