Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 71d2529

Browse files
Merge pull request SharingSource#220 from SharingSource/ac_oier
✨feat: Add 677
2 parents f2b72e6 + 20e6e01 commit 71d2529

File tree

4 files changed

+100
-0
lines changed

4 files changed

+100
-0
lines changed

‎Index/DFS.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
| [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/) | 困难 | 🤩🤩🤩🤩 |
1515
| [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/) | 中等 | 🤩🤩🤩🤩 |
1616
| [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/) | 中等 | 🤩🤩🤩🤩 |
17+
| [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/) | 中等 | 🤩🤩🤩🤩 |
1718
| [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/) | 简单 | 🤩🤩🤩 |
1819
| [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/) | 困难 | 🤩🤩🤩 |
1920
| [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/) | 简单 | 🤩🤩🤩 |

‎Index/哈希表.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
| [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/) | 简单 | 🤩🤩🤩🤩 |
2323
| [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/) | 中等 | 🤩🤩🤩 |
2424
| [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/) | 简单 | 🤩🤩🤩 |
25+
| [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/) | 中等 | 🤩🤩🤩🤩 |
2526
| [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/) | 中等 | 🤩🤩🤩 |
2627
| [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/) | 简单 | 🤩🤩🤩 |
2728
| [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/) | 简单 | 🤩🤩🤩🤩 |

‎Index/字典树.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@
33
| [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/) | 中等 | 🤩🤩🤩🤩🤩 |
44
| [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/) | 中等 | 🤩🤩🤩🤩🤩 |
55
| [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/) | 困难 | 🤩🤩🤩🤩 |
6+
| [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/) | 中等 | 🤩🤩🤩🤩 |
67
| [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/) | 困难 | 🤩🤩🤩 |
78

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
### 题目描述
2+
3+
这是 LeetCode 上的 **[677. 键值映射](https://leetcode-cn.com/problems/map-sum-pairs/solution/gong-shui-san-xie-jie-he-dfs-de-trie-yun-i4xa/)** ,难度为 **中等**
4+
5+
Tag : 「字典树」、「DFS」、「哈希表」
6+
7+
8+
9+
实现一个 MapSum 类,支持两个方法,insert 和 sum:
10+
11+
* `MapSum()` 初始化 `MapSum` 对象
12+
* `void insert(String key, int val)` 插入 `key-val` 键值对,字符串表示键 `key` ,整数表示值 `val` 。如果键 `key` 已经存在,那么原来的键值对将被替代成新的键值对。
13+
* `int sum(string prefix)` 返回所有以该前缀 `prefix` 开头的键 `key` 的值的总和。
14+
15+
示例:
16+
```
17+
输入:
18+
["MapSum", "insert", "sum", "insert", "sum"]
19+
[[], ["apple", 3], ["ap"], ["app", 2], ["ap"]]
20+
21+
输出:
22+
[null, null, 3, null, 5]
23+
24+
解释:
25+
MapSum mapSum = new MapSum();
26+
mapSum.insert("apple", 3);
27+
mapSum.sum("ap"); // return 3 (apple = 3)
28+
mapSum.insert("app", 2);
29+
mapSum.sum("ap"); // return 5 (apple + app = 3 + 2 = 5)
30+
```
31+
32+
提示:
33+
* `1 <= key.length, prefix.length <= 50`
34+
* `key``prefix` 仅由小写英文字母组成
35+
* `1 <= val <= 1000`
36+
* 最多调用 `50``insert``sum`
37+
38+
---
39+
40+
### Trie + DFS
41+
42+
从需要实现「存储字符串(映射关系)」并「检索某个字符串前缀的总和」来看,可以知道这是与 $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)
43+
44+
考虑如何实现两个操作:
45+
46+
* `insert` :在基本的 $Trie$ 插入操作的基础上进行拓展即可。与常规的插入操作的唯一区别为,不能简单记录单词的结束位置,还要存储 $key$ 对应的 $val$ 是多少。具体的我们可以使用 `int` 类型的数组 $hash$ 来代替原有的 `boolean` 类型的数组 $isWord$;
47+
48+
* `sum` : 先对入参 $prefix$ 进行字典树搜索,到达尾部后再使用 `DFS` 搜索后面的所有方案,并累加结果。
49+
50+
代码:
51+
```Java
52+
class MapSum {
53+
int[][] tr = new int[2510][26];
54+
int[] hash = new int[2510];
55+
int idx;
56+
public void insert(String key, int val) {
57+
int p = 0;
58+
for (int i = 0; i < key.length(); i++) {
59+
int u = key.charAt(i) - 'a';
60+
if (tr[p][u] == 0) tr[p][u] = ++idx;
61+
p = tr[p][u];
62+
}
63+
hash[p] = val;
64+
}
65+
public int sum(String prefix) {
66+
int p = 0;
67+
for (int i = 0; i < prefix.length(); i++) {
68+
int u = prefix.charAt(i) - 'a';
69+
if (tr[p][u] == 0) return 0;
70+
p = tr[p][u];
71+
}
72+
return dfs(p);
73+
}
74+
int dfs(int p) {
75+
int ans = hash[p];
76+
for (int u = 0; u < 26; u++) {
77+
if (tr[p][u] != 0) ans += dfs(tr[p][u]);
78+
}
79+
return ans;
80+
}
81+
}
82+
```
83+
* 时间复杂度:令 $key$ 的最大长度为 $n,ドル最大调用次数为 $m,ドル字符集大小为 $C$( 本题 $C$ 固定为 26ドル$ ),`insert` 操作的复杂度为 $O(n)$;从 `DFS` 的角度分析,`sum` 操作的复杂度为 $O(C^n),ドル但事实上,对于本题具有明确的计算量上界,搜索所有的格子的复杂度为 $O(n * m * C)$
84+
* 空间复杂度:$O(n * m * C)$
85+
86+
---
87+
88+
### 最后
89+
90+
这是我们「刷穿 LeetCode」系列文章的第 `No.677` 篇,系列开始于 2021年01月01日,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。
91+
92+
在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。
93+
94+
为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode
95+
96+
在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。
97+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /