图论27(Leetcode721账户合并)

代码:

写了一个超时版本 又学了并查集

超时版本:

java 复制代码
class Solution {
    public List<List<String>> accountsMerge(List<List<String>> accounts) {
        List<List<String>> newAcc = new ArrayList<>();
        Set<Integer> isArrive = new HashSet<>();
        int idx = 0;
        
        Iterator<List<String>> iterator = accounts.iterator();
        while(iterator.hasNext()){
            List<String> curAcc = iterator.next();
            if(isArrive.contains(idx)){
                idx++;
                continue;
            }
            Set<Integer> set = new HashSet<>();
            set.add(idx);
            set = getIdx(idx,set,accounts);
            List<String> curEmail = new ArrayList<>();
            for(int i:set){
                for(String str:accounts.get(i)){
                    if(!curEmail.contains(str))curEmail.add(str);
                }
            }
            if(!isArrive.contains(idx)){
                String name = curEmail.get(0);
                curEmail.remove(0);
                Collections.sort(curEmail); 
                curEmail.add(0,name);
                newAcc.add(curEmail);
                isArrive.add(idx);
                for(int i:set){
                    isArrive.add(i);
                }
            }
            idx++;
        }        
        return newAcc;

    }
    public Set<Integer> getIdx(int curIdx, Set<Integer> set, List<List<String>> accounts){
        List<String> curAccount = accounts.get(curIdx);
        // curAccount.remove(0);
        for(String email:curAccount){
            for(int i=0;i<accounts.size();i++){
                if(set.contains(i))continue;
                List<String> acc = accounts.get(i);
                if(acc.contains(email)&&email.contains("@")){
                    set.add(i);
                    set = getIdx(i,set,accounts);
                }
            }
        }
        return set;
    }
}

并查集版:

java 复制代码
class Solution {
    public List<List<String>> accountsMerge(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){//遍历 写email的序号 和 email的name对
            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()){//把相同根节点的email放一起
            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<>();
            account.add(name);
            account.addAll(emails);
            merged.add(account);
        }
        return merged;
    }
}
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];
    }
}
相关推荐
JingHongB3 小时前
代码随想录算法训练营Day55 | 图论理论基础、深度优先搜索理论基础、卡玛网 98.所有可达路径、797. 所有可能的路径、广度优先搜索理论基础
算法·深度优先·图论
weixin_432702263 小时前
代码随想录算法训练营第五十五天|图论理论基础
数据结构·python·算法·深度优先·图论
小冉在学习3 小时前
day52 图论章节刷题Part04(110.字符串接龙、105.有向图的完全可达性、106.岛屿的周长 )
算法·深度优先·图论
小冉在学习3 小时前
day53 图论章节刷题Part05(并查集理论基础、寻找存在的路径)
java·算法·图论
chan_lay2 天前
图论导引 - 目录、引言、第一章 - 11/05
笔记·图论
£suPerpanda2 天前
牛客周赛 Round65 补题DEF
开发语言·数据结构·c++·算法·深度优先·动态规划·图论
c沫栀3 天前
E-小H学历史(牛客练习赛131)
c++·算法·深度优先·图论
小冉在学习3 天前
day50 图论章节刷题Part02(99.岛屿数量 深搜、99.岛屿数量 广搜、100.岛屿的最大面积)
图论
weixin_478689763 天前
【图论】——理论基础总结
图论
夜雨翦春韭3 天前
【代码随想录Day60】图论Part11
java·数据结构·算法·leetcode·图论