LeetCode 题解工作台
账户合并
给定一个列表 accounts ,每个元素 accounts[i] 是一个字符串列表,其中第一个元素 accounts[i][0] 是 名称 (name) ,其余元素是 emails 表示该账户的邮箱地址。 现在,我们想合并这些账户。如果两个账户都有一些共同的邮箱地址,则两个账户必定属于同一个人。请…
7
题型
5
代码语言
3
相关题
当前训练重点
中等 · 数组·哈希·扫描
答案摘要
根据题目描述,我们可以使用并查集,将具有相同邮箱地址的账户合并在一起。 我们首先遍历所有的账户,对于第 个账户,我们遍历其所有的邮箱地址,如果该邮箱地址在哈希表 中出现过,则使用并查集,将该账户的编号 与之前出现过的邮箱地址所属的账户编号进行合并;否则,将该邮箱地址与账户的编号 进行映射。
Interview AiBoxInterview AiBox 实时 AI 助手,陪你讲清 数组·哈希·扫描 题型思路
题目描述
给定一个列表 accounts,每个元素 accounts[i] 是一个字符串列表,其中第一个元素 accounts[i][0] 是 名称 (name),其余元素是 emails 表示该账户的邮箱地址。
现在,我们想合并这些账户。如果两个账户都有一些共同的邮箱地址,则两个账户必定属于同一个人。请注意,即使两个账户具有相同的名称,它们也可能属于不同的人,因为人们可能具有相同的名称。一个人最初可以拥有任意数量的账户,但其所有账户都具有相同的名称。
合并账户后,按以下格式返回账户:每个账户的第一个元素是名称,其余元素是 按字符 ASCII 顺序排列 的邮箱地址。账户本身可以以 任意顺序 返回。
示例 1:
输入:accounts = [["John", "johnsmith@mail.com", "john00@mail.com"], ["John", "johnnybravo@mail.com"], ["John", "johnsmith@mail.com", "john_newyork@mail.com"], ["Mary", "mary@mail.com"]] 输出:[["John", 'john00@mail.com', 'john_newyork@mail.com', 'johnsmith@mail.com'], ["John", "johnnybravo@mail.com"], ["Mary", "mary@mail.com"]] 解释: 第一个和第三个 John 是同一个人,因为他们有共同的邮箱地址 "johnsmith@mail.com"。 第二个 John 和 Mary 是不同的人,因为他们的邮箱地址没有被其他帐户使用。 可以以任何顺序返回这些列表,例如答案 [['Mary','mary@mail.com'],['John','johnnybravo@mail.com'], ['John','john00@mail.com','john_newyork@mail.com','johnsmith@mail.com']] 也是正确的。
示例 2:
输入:accounts = [["Gabe","Gabe0@m.co","Gabe3@m.co","Gabe1@m.co"],["Kevin","Kevin3@m.co","Kevin5@m.co","Kevin0@m.co"],["Ethan","Ethan5@m.co","Ethan4@m.co","Ethan0@m.co"],["Hanzo","Hanzo3@m.co","Hanzo1@m.co","Hanzo0@m.co"],["Fern","Fern5@m.co","Fern1@m.co","Fern0@m.co"]] 输出:[["Ethan","Ethan0@m.co","Ethan4@m.co","Ethan5@m.co"],["Gabe","Gabe0@m.co","Gabe1@m.co","Gabe3@m.co"],["Hanzo","Hanzo0@m.co","Hanzo1@m.co","Hanzo3@m.co"],["Kevin","Kevin0@m.co","Kevin3@m.co","Kevin5@m.co"],["Fern","Fern0@m.co","Fern1@m.co","Fern5@m.co"]]
提示:
1 <= accounts.length <= 10002 <= accounts[i].length <= 101 <= accounts[i][j].length <= 30accounts[i][0]由英文字母组成accounts[i][j] (for j > 0)是有效的邮箱地址
解题思路
方法一:并查集 + 哈希表
根据题目描述,我们可以使用并查集,将具有相同邮箱地址的账户合并在一起。
我们首先遍历所有的账户,对于第 个账户,我们遍历其所有的邮箱地址,如果该邮箱地址在哈希表 中出现过,则使用并查集,将该账户的编号 与之前出现过的邮箱地址所属的账户编号进行合并;否则,将该邮箱地址与账户的编号 进行映射。
接下来,我们遍历所有的账户,对于第 个账户,我们使用并查集找到其根节点,然后将该账户的所有邮箱地址添加到哈希表 中,其中键为根节点,值为该账户的所有邮箱地址。
时间复杂度 ,空间复杂度 。其中 为账户的数量。
class UnionFind:
def __init__(self, n):
self.p = list(range(n))
self.size = [1] * n
def find(self, x):
if self.p[x] != x:
self.p[x] = self.find(self.p[x])
return self.p[x]
def union(self, a, b):
pa, pb = self.find(a), self.find(b)
if pa == pb:
return False
if self.size[pa] > self.size[pb]:
self.p[pb] = pa
self.size[pa] += self.size[pb]
else:
self.p[pa] = pb
self.size[pb] += self.size[pa]
return True
class Solution:
def accountsMerge(self, accounts: List[List[str]]) -> List[List[str]]:
uf = UnionFind(len(accounts))
d = {}
for i, (_, *emails) in enumerate(accounts):
for email in emails:
if email in d:
uf.union(i, d[email])
else:
d[email] = i
g = defaultdict(set)
for i, (_, *emails) in enumerate(accounts):
root = uf.find(i)
g[root].update(emails)
return [[accounts[root][0]] + sorted(emails) for root, emails in g.items()]
复杂度分析
| 指标 | 值 |
|---|---|
| 时间 | complexity is O(NK log NK) where N is the number of accounts and K is the maximum emails per account due to sorting. Space complexity is O(NK) for storing the graph and visited flags for emails. |
| 空间 | O(NK) |
面试官常问的追问
外企场景- question_mark
Building a graph of emails is expected, indicating awareness of array scanning plus hash lookup.
- question_mark
Mentioning DFS, BFS, or Union-Find shows proper connected component handling.
- question_mark
Sorting emails before returning results indicates attention to problem-specific output requirements.
常见陷阱
外企场景- error
Assuming same name implies same person, which fails when names repeat but emails differ.
- error
Forgetting to remove duplicate emails when merging accounts.
- error
Not linking all emails in the same account, leading to disconnected components and incomplete merges.
进阶变体
外企场景- arrow_right_alt
Accounts Merge II with phone numbers instead of emails.
- arrow_right_alt
Accounts Merge with dynamic account additions in real-time streaming.
- arrow_right_alt
Accounts Merge with additional constraints on maximum account size or email count.