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 066b1ad

Browse files
✨feat: Modify 面试题 10.02
1 parent 634e325 commit 066b1ad

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

‎LeetCode/面试题/面试题 10.02. 变位词组(中等).md‎

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
这是 LeetCode 上的 **[面试题 10.02. 变位词组](https://leetcode-cn.com/problems/group-anagrams-lcci/solution/gong-shui-san-xie-tong-ji-bian-wei-ci-de-0iqe/)** ,难度为 **中等**
44

5-
Tag : 「哈希表」、「排序」、「计数」、「数学」
5+
Tag : 「哈希表」、「排序」、「计数」、「数学」、「打表」
66

77

88

@@ -113,17 +113,17 @@ class Solution {
113113
}
114114
public List<List<String>> groupAnagrams(String[] ss) {
115115
List<List<String>> ans = new ArrayList<>();
116-
Map<Integer, List<String>> map = new HashMap<>();
116+
Map<Long, List<String>> map = new HashMap<>();
117117
for (String s : ss) {
118-
int cur = 1;
118+
long cur = 1;
119119
for (char c : s.toCharArray()) {
120120
cur *= nums[c - 'a'];
121121
}
122122
List<String> list = map.getOrDefault(cur, new ArrayList<>());
123123
list.add(s);
124124
map.put(cur, list);
125125
}
126-
for (int key : map.keySet()) ans.add(map.get(key));
126+
for (long key : map.keySet()) ans.add(map.get(key));
127127
return ans;
128128
}
129129
}
@@ -133,14 +133,15 @@ class Solution {
133133

134134
---
135135

136-
### 我猜你问
136+
### 溢出说明
137137

138-
* 方法三为何可以确保不溢出?
138+
使用 `long` 仍然存在溢出风险,但使用"长度不受限制"的高精度哈希值实现是不现实的。
139139

140-
这里的不溢出分析可以基于两点:
140+
哈希值必须是有限值域内,才有意义。
141141

142-
1. 一道相似的题目:[49. 字母异位词分组](https://leetcode-cn.com/problems/group-anagrams/),里面有写对应的数据范围;
143-
2. 利用已经确定可过的算法,反推数据范围:在方法一和方法二都可过的情况下,我们可以知道 $\sum_{i = 0}^{n - 1}ss[i].length <= 10^6,ドル最坏情况下,$ss$ 只有一个字符串时,取得最大乘积,第 26ドル$ 位质数大概为 10ドル^2$ 左右,字符串长度为 10ドル^6,ドル乘积确保在 10ドル^9$ 内,不会溢出。
142+
换句话说,如果使用高精度的哈希值的话,我们是无法直接将两个哈希值进行异或判断结果是否为 0ドル$ 来得出哈希值是否相同的结论,而是需要使用 $O(Len)$ 的复杂度来判定哈希值是否相同。
143+
144+
因此,针对存在的哈希冲突问题,要么是解决冲突;要是使用与「字符串哈希」类似的做法,不处理溢出(相当于模 2ドル^{64}$),但这样会存在溢出次数不一样的值对应的哈希值相同的问题,只能说是一种期望冲突不发生的做法。
144145

145146
---
146147

0 commit comments

Comments
(0)

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