Ai
1 Star 2 Fork 5

LilithSangreal/LeetCode-Solutions

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
accounts-merge.py 1.60 KB
一键复制 编辑 原始数据 按行查看 历史
Allen Liu 提交于 2018-10-13 13:15 +08:00 . remove unused code
# Time: O(nlogn), n is the number of total emails,
# and the max length ofemail is 320, p.s. {64}@{255}
# Space: O(n)
import collections
class UnionFind(object):
def __init__(self):
self.set = []
def get_id(self):
self.set.append(len(self.set))
return len(self.set)-1
def find_set(self, x):
if self.set[x] != x:
self.set[x] = self.find_set(self.set[x]) # path compression.
return self.set[x]
def union_set(self, x, y):
x_root, y_root = map(self.find_set, (x, y))
if x_root != y_root:
self.set[min(x_root, y_root)] = max(x_root, y_root)
class Solution(object):
def accountsMerge(self, accounts):
"""
:type accounts: List[List[str]]
:rtype: List[List[str]]
"""
union_find = UnionFind()
email_to_name = {}
email_to_id = {}
for account in accounts:
name = account[0]
for i in xrange(1, len(account)):
if account[i] not in email_to_id:
email_to_name[account[i]] = name
email_to_id[account[i]] = union_find.get_id()
union_find.union_set(email_to_id[account[1]],
email_to_id[account[i]])
result = collections.defaultdict(list)
for email in email_to_name.keys():
result[union_find.find_set(email_to_id[email])].append(email)
for emails in result.values():
emails.sort()
return [[email_to_name[emails[0]]] + emails
for emails in result.values()]
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/LilithSangreal/LeetCode-Solutions.git
git@gitee.com:LilithSangreal/LeetCode-Solutions.git
LilithSangreal
LeetCode-Solutions
LeetCode-Solutions
master

搜索帮助