码迷,mamicode.com
首页 > 其他好文 > 详细

Leetcode 721: Accounts Merge

时间:2018-01-29 11:47:23      阅读:201      评论:0      收藏:0      [点我收藏+]

标签:des   amp   each   sel   log   div   res   hat   name   

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 emails representing 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].

 

 

 1 public class Solution {
 2     public IList<IList<string>> AccountsMerge(IList<IList<string>> accounts) {
 3         var result = new List<IList<string>>();
 4         
 5         if (accounts == null || accounts.Count == 0) return result;
 6         
 7         // email address to list of accounts
 8         var dict = new Dictionary<string, HashSet<int>>();
 9         
10         // build the graph
11         for (int j = 0; j < accounts.Count; j++)
12         {
13             var account = accounts[j];
14             for (int i = 1; i < account.Count; i++)
15             {
16                 if (!dict.ContainsKey(account[i]))
17                 {
18                     dict[account[i]] = new HashSet<int>();
19                 }
20                 
21                 dict[account[i]].Add(j);
22             }
23         }
24         
25         var visited = new HashSet<string>();
26         
27         foreach (var email in dict.Keys)
28         {
29             if (visited.Contains(email)) continue;
30             visited.Add(email);
31             
32             var list = new List<string>();
33             list.Add(email);
34             
35             var name = "";
36             var queue = new Queue<string>();
37             
38             queue.Enqueue(email);
39             
40             while (queue.Count > 0)
41             {
42                 var e = queue.Dequeue();
43                 
44                 foreach (var aId in dict[e])
45                 {
46                     var account = accounts[aId];
47                     name = account[0];
48                     
49                     for (int i = 1; i < account.Count; i++)
50                     {
51                         if (!visited.Contains(account[i]))
52                         {
53                             queue.Enqueue(account[i]);
54                             list.Add(account[i]);
55                             visited.Add(account[i]);
56                         }
57                     }
58                 }
59             }
60             
61             list.Insert(0, name);
62             
63             // c# string comparer has different behavior than Java
64             list.Sort((Comparison<String>) (
65                 (String left, String right) => {
66                     return String.CompareOrdinal(left, right);
67                 }
68             ));
69             
70             result.Add(list);
71         }
72         
73         return result;
74     }
75 }

 

Leetcode 721: Accounts Merge

标签:des   amp   each   sel   log   div   res   hat   name   

原文地址:https://www.cnblogs.com/liangmou/p/8375751.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!