Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Index/DFS.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
| [488. 祖玛游戏](https://leetcode-cn.com/problems/zuma-game/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/zuma-game/solution/gong-shui-san-xie-yi-ti-shuang-jie-sou-s-3ftb/) | 困难 | 🤩🤩🤩🤩 |
| [494. 目标和](https://leetcode-cn.com/problems/target-sum/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/target-sum/solution/gong-shui-san-xie-yi-ti-si-jie-dfs-ji-yi-et5b/) | 中等 | 🤩🤩🤩🤩 |
| [638. 大礼包](https://leetcode-cn.com/problems/shopping-offers/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/shopping-offers/solution/gong-shui-san-xie-yi-ti-shuang-jie-zhuan-qgk1/) | 中等 | 🤩🤩🤩🤩 |
| [677. 键值映射](https://leetcode-cn.com/problems/map-sum-pairs/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/map-sum-pairs/solution/gong-shui-san-xie-jie-he-dfs-de-trie-yun-i4xa/) | 中等 | 🤩🤩🤩🤩 |
| [690. 员工的重要性](https://leetcode-cn.com/problems/employee-importance/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/employee-importance/solution/gong-shui-san-xie-yi-ti-shuang-jie-di-gu-s79x/) | 简单 | 🤩🤩🤩 |
| [778. 水位上升的泳池中游泳](https://leetcode-cn.com/problems/swim-in-rising-water/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/swim-in-rising-water/solution/gong-shui-san-xie-yi-ti-shuang-jie-krusk-7c6o/) | 困难 | 🤩🤩🤩 |
| [783. 二叉搜索树节点最小距离](https://leetcode-cn.com/problems/minimum-distance-between-bst-nodes/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/minimum-distance-between-bst-nodes/solution/gong-shui-san-xie-yi-ti-san-jie-shu-de-s-7r17/) | 简单 | 🤩🤩🤩 |
Expand Down
1 change: 1 addition & 0 deletions Index/哈希表.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
| [500. 键盘行](https://leetcode-cn.com/problems/keyboard-row/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/keyboard-row/solution/gong-shui-san-xie-jian-dan-zi-fu-chuan-m-zx6b/) | 简单 | 🤩🤩🤩🤩 |
| [554. 砖墙](https://leetcode-cn.com/problems/brick-wall/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/brick-wall/solution/gong-shui-san-xie-zheng-nan-ze-fan-shi-y-gsri/) | 中等 | 🤩🤩🤩 |
| [645. 错误的集合](https://leetcode-cn.com/problems/set-mismatch/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/set-mismatch/solution/gong-shui-san-xie-yi-ti-san-jie-ji-shu-s-vnr9/) | 简单 | 🤩🤩🤩 |
| [677. 键值映射](https://leetcode-cn.com/problems/map-sum-pairs/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/map-sum-pairs/solution/gong-shui-san-xie-jie-he-dfs-de-trie-yun-i4xa/) | 中等 | 🤩🤩🤩🤩 |
| [692. 前K个高频单词](https://leetcode-cn.com/problems/top-k-frequent-words/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/top-k-frequent-words/solution/gong-shui-san-xie-xiang-jie-shi-yong-ha-8dxt2/) | 中等 | 🤩🤩🤩 |
| [697. 数组的度](https://leetcode-cn.com/problems/degree-of-an-array/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/degree-of-an-array/solution/shu-zu-ji-shu-ha-xi-biao-ji-shu-jie-fa-y-a0mg/) | 简单 | 🤩🤩🤩 |
| [705. 设计哈希集合](https://leetcode-cn.com/problems/design-hashset/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/design-hashset/solution/yi-ti-san-jie-jian-dan-shu-zu-lian-biao-nj3dg/) | 简单 | 🤩🤩🤩🤩 |
Expand Down
1 change: 1 addition & 0 deletions Index/字典树.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
| [208. 实现 Trie (前缀树)](https://leetcode-cn.com/problems/implement-trie-prefix-tree/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/implement-trie-prefix-tree/solution/gong-shui-san-xie-yi-ti-shuang-jie-er-we-esm9/) | 中等 | 🤩🤩🤩🤩🤩 |
| [211. 添加与搜索单词 - 数据结构设计](https://leetcode-cn.com/problems/design-add-and-search-words-data-structure/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/design-add-and-search-words-data-structure/solution/gong-shui-san-xie-yi-ti-shuang-jie-er-we-un94/) | 中等 | 🤩🤩🤩🤩🤩 |
| [212. 单词搜索 II](https://leetcode-cn.com/problems/word-search-ii/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/word-search-ii/solution/gong-shui-san-xie-yi-ti-shuang-jie-hui-s-am8f/) | 困难 | 🤩🤩🤩🤩 |
| [677. 键值映射](https://leetcode-cn.com/problems/map-sum-pairs/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/map-sum-pairs/solution/gong-shui-san-xie-jie-he-dfs-de-trie-yun-i4xa/) | 中等 | 🤩🤩🤩🤩 |
| [1707. 与数组中元素的最大异或值](https://leetcode-cn.com/problems/maximum-xor-with-an-element-from-array/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/maximum-xor-with-an-element-from-array/solution/gong-shui-san-xie-jie-zhe-ge-wen-ti-lai-lypqr/) | 困难 | 🤩🤩🤩 |

97 changes: 97 additions & 0 deletions LeetCode/671-680/677. 键值映射(中等).md
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
### 题目描述

这是 LeetCode 上的 **[677. 键值映射](https://leetcode-cn.com/problems/map-sum-pairs/solution/gong-shui-san-xie-jie-he-dfs-de-trie-yun-i4xa/)** ,难度为 **中等**。

Tag : 「字典树」、「DFS」、「哈希表」



实现一个 MapSum 类,支持两个方法,insert 和 sum:

* `MapSum()` 初始化 `MapSum` 对象
* `void insert(String key, int val)` 插入 `key-val` 键值对,字符串表示键 `key` ,整数表示值 `val` 。如果键 `key` 已经存在,那么原来的键值对将被替代成新的键值对。
* `int sum(string prefix)` 返回所有以该前缀 `prefix` 开头的键 `key` 的值的总和。

示例:
```
输入:
["MapSum", "insert", "sum", "insert", "sum"]
[[], ["apple", 3], ["ap"], ["app", 2], ["ap"]]

输出:
[null, null, 3, null, 5]

解释:
MapSum mapSum = new MapSum();
mapSum.insert("apple", 3);
mapSum.sum("ap"); // return 3 (apple = 3)
mapSum.insert("app", 2);
mapSum.sum("ap"); // return 5 (apple + app = 3 + 2 = 5)
```

提示:
* `1 <= key.length, prefix.length <= 50`
* `key` 和 `prefix` 仅由小写英文字母组成
* `1 <= val <= 1000`
* 最多调用 `50` 次 `insert` 和 `sum`

---

### Trie + DFS

从需要实现「存储字符串(映射关系)」并「检索某个字符串前缀的总和」来看,可以知道这是与 $Trie$ 相关的题目,还不了解 $Trie$ 的同学可以先看前置 🧀:[实现 Trie (前缀树)](https://mp.weixin.qq.com/s?__biz=MzU4NDE3MTEyMA==&mid=2247488490&idx=1&sn=db2998cb0e5f08684ee1b6009b974089&chksm=fd9cb8f5caeb31e3f7f67dba981d8d01a24e26c93ead5491edb521c988adc0798d8acb6f9e9d&token=59039721&lang=zh_CN#rd) 。

考虑如何实现两个操作:

* `insert` :在基本的 $Trie$ 插入操作的基础上进行拓展即可。与常规的插入操作的唯一区别为,不能简单记录单词的结束位置,还要存储 $key$ 对应的 $val$ 是多少。具体的我们可以使用 `int` 类型的数组 $hash$ 来代替原有的 `boolean` 类型的数组 $isWord$;

* `sum` : 先对入参 $prefix$ 进行字典树搜索,到达尾部后再使用 `DFS` 搜索后面的所有方案,并累加结果。

代码:
```Java
class MapSum {
int[][] tr = new int[2510][26];
int[] hash = new int[2510];
int idx;
public void insert(String key, int val) {
int p = 0;
for (int i = 0; i < key.length(); i++) {
int u = key.charAt(i) - 'a';
if (tr[p][u] == 0) tr[p][u] = ++idx;
p = tr[p][u];
}
hash[p] = val;
}
public int sum(String prefix) {
int p = 0;
for (int i = 0; i < prefix.length(); i++) {
int u = prefix.charAt(i) - 'a';
if (tr[p][u] == 0) return 0;
p = tr[p][u];
}
return dfs(p);
}
int dfs(int p) {
int ans = hash[p];
for (int u = 0; u < 26; u++) {
if (tr[p][u] != 0) ans += dfs(tr[p][u]);
}
return ans;
}
}
```
* 时间复杂度:令 $key$ 的最大长度为 $n$,最大调用次数为 $m$,字符集大小为 $C$( 本题 $C$ 固定为 $26$ ),`insert` 操作的复杂度为 $O(n)$;从 `DFS` 的角度分析,`sum` 操作的复杂度为 $O(C^n)$,但事实上,对于本题具有明确的计算量上界,搜索所有的格子的复杂度为 $O(n * m * C)$
* 空间复杂度:$O(n * m * C)$

---

### 最后

这是我们「刷穿 LeetCode」系列文章的第 `No.677` 篇,系列开始于 2021/01/01,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。

在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。

为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode 。

在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。