1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
| import java.util.*;
public class _721账户合并 {
public List<List<String>> accountsMerge_gov(List<List<String>> accounts) { Map<String, Integer> emailToIndex = new HashMap<String, Integer>(); Map<String, String> emailToName = new HashMap<String, String>(); int emailsCount = 0; for (List<String> account : accounts) { String name = account.get(0); int size = account.size(); for (int i = 1; i < size; i++) { String email = account.get(i); if (!emailToIndex.containsKey(email)) { emailToIndex.put(email, emailsCount++); emailToName.put(email, name); } } } UnionFind uf = new UnionFind(emailsCount); for (List<String> account : accounts) { String firstEmail = account.get(1); int firstIndex = emailToIndex.get(firstEmail); int size = account.size(); for (int i = 2; i < size; i++) { String nextEmail = account.get(i); int nextIndex = emailToIndex.get(nextEmail); uf.union(firstIndex, nextIndex); } } Map<Integer, List<String>> indexToEmails = new HashMap<Integer, List<String>>(); for (String email : emailToIndex.keySet()) { int index = uf.find(emailToIndex.get(email)); List<String> account = indexToEmails.getOrDefault(index, new ArrayList<String>()); account.add(email); indexToEmails.put(index, account); } List<List<String>> merged = new ArrayList<List<String>>(); for (List<String> emails : indexToEmails.values()) { Collections.sort(emails); String name = emailToName.get(emails.get(0)); List<String> account = new ArrayList<String>(); account.add(name); account.addAll(emails); merged.add(account); } return merged; }
static class UnionFind { int[] parent;
public UnionFind(int n) { parent = new int[n]; for (int i = 0; i < n; i++) { parent[i] = i; } }
public void union(int index1, int index2) { parent[find(index2)] = find(index1); }
public int find(int index) { if (parent[index] != index) { parent[index] = find(parent[index]); } return parent[index]; } }
public List<List<String>> accountsMerge(List<List<String>> accounts) { Map<String, Set<String>> mapAccount = new HashMap<>(); Map<String, String> mapEmail = new HashMap<>();
for (int i = 0; i < accounts.size(); i++) { List<String> strings = accounts.get(i); Set<String> temp = new HashSet<>(); Set<String> set = new HashSet<>(strings.subList(1, strings.size())); String name = i + "-" + strings.getFirst(); for (String email : set) { if (mapEmail.containsKey(email)) { String nameBefore = mapEmail.get(email); if (mapAccount.containsKey(nameBefore)) { temp.addAll(mapAccount.get(nameBefore)); mapAccount.remove(nameBefore); } } mapEmail.put(email, name); }
for (String email : temp) { mapEmail.put(email, name); } temp.addAll(set); mapAccount.put(name, temp); } List<List<String>> res = new ArrayList<>(); mapAccount.forEach((key, value) -> { List<String> list = new ArrayList<>(value); Collections.sort(list); list.addFirst(key.split("-")[1]); res.add(list); }); return res; }
public static void main(String[] args) { _721账户合并 accountMerge = new _721账户合并(); List<List<String>> accounts = new ArrayList<>();
accounts.add(new ArrayList<>(List.of("Isa", "Isa6@m.co", "Isa0@m.co", "Isa1@m.co", "Isa6@m.co", "Isa1@m.co"))); accounts.add(new ArrayList<>(List.of("David", "David10@m.co", "David12@m.co", "David7@m.co", "David6@m.co", "David1@m.co"))); accounts.add(new ArrayList<>(List.of("Alex", "Alex1@m.co", "Alex7@m.co", "Alex6@m.co", "Alex11@m.co", "Alex2@m.co"))); accounts.add(new ArrayList<>(List.of("Hanzo", "Hanzo11@m.co", "Hanzo2@m.co", "Hanzo5@m.co", "Hanzo5@m.co", "Hanzo3@m.co"))); accounts.add(new ArrayList<>(List.of("Bob", "Bob12@m.co", "Bob1@m.co", "Bob12@m.co", "Bob7@m.co", "Bob5@m.co"))); accounts.add(new ArrayList<>(List.of("Isa", "Isa4@m.co", "Isa1@m.co", "Isa3@m.co", "Isa9@m.co", "Isa2@m.co"))); accounts.add(new ArrayList<>(List.of("Kevin", "Kevin0@m.co", "Kevin7@m.co", "Kevin12@m.co", "Kevin3@m.co", "Kevin5@m.co"))); accounts.add(new ArrayList<>(List.of("David", "David12@m.co", "David6@m.co", "David10@m.co", "David0@m.co", "David8@m.co"))); accounts.add(new ArrayList<>(List.of("Celine", "Celine0@m.co", "Celine8@m.co", "Celine10@m.co", "Celine2@m.co", "Celine12@m.co"))); accounts.add(new ArrayList<>(List.of("Kevin", "Kevin1@m.co", "Kevin8@m.co", "Kevin5@m.co", "Kevin9@m.co", "Kevin0@m.co"))); accounts.add(new ArrayList<>(List.of("Alex", "Alex3@m.co", "Alex8@m.co", "Alex1@m.co", "Alex12@m.co", "Alex12@m.co"))); accounts.add(new ArrayList<>(List.of("Kevin", "Kevin8@m.co", "Kevin12@m.co", "Kevin1@m.co", "Kevin10@m.co", "Kevin9@m.co")));
for (List<String> strings : accountMerge.accountsMerge(accounts)) { System.out.println(strings); } } }
|