最近刷LeetCode题目的一些思路,题目信息
给定一个列表 accounts
,每个元素 accounts[i]
是一个字符串列表,其中第一个元素 accounts[i][0]
是 名称 (name),其余元素是 emails 表示该帐户的邮箱地址。
现在,我们想合并这些帐户。如果两个帐户都有一些共同的邮件地址,则两个帐户必定属于同一个人。请注意,即使两个帐户具有相同的名称,它们也可能属于不同的人,因为人们可能具有相同的名称。一个人最初可以拥有任意数量的帐户,但其所有帐户都具有相同的名称。
合并帐户后,按以下格式返回帐户:每个帐户的第一个元素是名称,其余元素是按顺序排列的邮箱地址。accounts 本身可以以任意顺序返回。
例子 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: 第一个和第三个 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']]仍然会被接受。
注意:
accounts
的长度将在[1,1000]
的范围内。accounts[i]
的长度将在[1,10]
的范围内。accounts[i][j]
的长度将在[1,30]
的范围内。
--------------------------------------------------------------------------------------
第一反应就是直接挨个比较,但是想想肯定是错的,然后看了下题目的相关话题,并查集,然后就明白该题的出题意图了,但是之前对并查集也仅仅是听说,如果没有搞清楚并查集的实现原理,这道题还真的没办法通过最优时间复杂度的算法进行计算,查找学习了并查集算法的原理和机制后,开始解题(关于并查集,已经有喝多好的文章了,以后如果有机会单独写一篇自己所理解的并查集)
1. 并查集一定用到的两个方法,寻根和并根,其中find方法是找到当前集合有关的根集合,union方法是合并,也可以当做关联,就是表明我这两个集合是相关联的
private int find(int[] array, int x){ if(array[x] != x){ x = array[x]; find(array,x); } return x; } private void union(int[] array,int from, int to){ int realFrom = find(array,from); int realTo = find(array,to); for(int i=0; i<array.length; i++){ if(array[i] == realFrom){ array[i] = realTo; } } }
2:既然有了并查集的方法,那么下面就是具体实现了,这个就相对直白和简单了
public List<List<String>> accountsMerge(List<List<String>> accounts) { List<List<String>> retList = new ArrayList<>(); int[] personArray = new int[accounts.size()]; for(int n=0; n<personArray.length; n++){ personArray[n] = n; } Map<String,Integer> emailHashMap = new HashMap<>(); Map<Integer,List<String>> positionHashMap = new HashMap<>(); for(int i=0; i<accounts.size(); i++){ for(int j=1; j<accounts.get(i).size(); j++){ if(emailHashMap.containsKey(accounts.get(i).get(j))){ union(personArray,i,emailHashMap.get(accounts.get(i).get(j))); }else { emailHashMap.put(accounts.get(i).get(j),i); } } } for(Map.Entry<String,Integer> entry: emailHashMap.entrySet()){ int personPosition = find(personArray,entry.getValue()); if(positionHashMap.get(personPosition) != null){ List<String> emailList = positionHashMap.get(personPosition); emailList.add(entry.getKey()); positionHashMap.put(personPosition,emailList); } else { List<String> emailList = new ArrayList<>(); emailList.add(entry.getKey()); positionHashMap.put(personPosition,emailList); } } for(int position: positionHashMap.keySet()){ List<String> personList = new ArrayList<>(); personList.addAll(positionHashMap.get(position)); personList.add(0,accounts.get(position).get(0)); Collections.sort(personList); retList.add(personList); } return retList; }