# halfrost/LeetCode-Go

Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
..
Failed to load latest commit information.
721. Accounts Merge.go
721. Accounts Merge_test.go

# 721. Accounts Merge

## 题目:

Given a list `accounts`, each element `accounts[i]` is a list of strings, where the first element `accounts[i][0]` is a name, and the rest of the elements are emailsrepresenting emails of the account.

Now, we would like to merge these accounts. Two accounts definitely belong to the same person if there is some email that is common to both accounts. Note that even if two accounts have the same name, they may belong to different people as people could have the same name. A person can have any number of accounts initially, but all of their accounts definitely have the same name.

After merging the accounts, return the accounts in the following format: the first element of each account is the name, and the rest of the elements are emails in sorted order. The accounts themselves can be returned in any order.

Example 1:

``````Input:
accounts = [["John", "johnsmith@mail.com", "john00@mail.com"], ["John", "johnnybravo@mail.com"], ["John", "johnsmith@mail.com", "john_newyork@mail.com"], ["Mary", "mary@mail.com"]]
Output: [["John", 'john00@mail.com', 'john_newyork@mail.com', 'johnsmith@mail.com'],  ["John", "johnnybravo@mail.com"], ["Mary", "mary@mail.com"]]
Explanation:
The first and third John's are the same person as they have the common email "johnsmith@mail.com".
The second John and Mary are different people as none of their email addresses are used by other accounts.
We could return these lists in any order, for example the answer [['Mary', 'mary@mail.com'], ['John', 'johnnybravo@mail.com'],
['John', 'john00@mail.com', 'john_newyork@mail.com', 'johnsmith@mail.com']] would still be accepted.
``````

Note:

• The length of `accounts` will be in the range `[1, 1000]`.
• The length of `accounts[i]` will be in the range `[1, 10]`.
• The length of `accounts[i][j]` will be in the range `[1, 30]`.

## 题目大意

• accounts 的长度将在 [1，1000] 的范围内。
• accounts[i] 的长度将在 [1，10] 的范围内。
• accounts[i][j] 的长度将在 [1，30] 的范围内。

## 解题思路

• 给出一堆账户和对应的邮箱。要求合并同一个人的多个邮箱账户。如果判断是同一个人呢？如果这个人名和所属的其中之一的邮箱是相同的，就判定这是同一个人的邮箱，那么就合并这些邮箱。
• 这题的解题思路是并查集。不过如果用暴力合并的方法，时间复杂度非常差。优化方法是先把每组数据都进行编号，人编号，每个邮箱都进行编号。这个映射关系用 `map` 记录起来。如果利用并查集的 `union()` 操作，把这些编号都进行合并。最后把人的编号和对应邮箱的编号拼接起来。
• 这一题有 2 处比较“坑”的是，不需要合并的用户的邮箱列表也是需要排序和去重的，同一个人的所有邮箱集合都要合并到一起。具体见测试用例。不过题目中也提到了这些点，也不能算题目坑，只能归自己没注意这些边界情况。
You can’t perform that action at this time.