图论27(Leetcode721账户合并)

代码:

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

超时版本:

class Solution {
    public List> accountsMerge(List> accounts) {
        List> newAcc = new ArrayList<>();
        Set isArrive = new HashSet<>();
        int idx = 0;
        
        Iterator> iterator = accounts.iterator();
        while(iterator.hasNext()){
            List curAcc = iterator.next();
            if(isArrive.contains(idx)){
                idx++;
                continue;
            }
            Set set = new HashSet<>();
            set.add(idx);
            set = getIdx(idx,set,accounts);
            List 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 getIdx(int curIdx, Set set, List> accounts){
        List curAccount = accounts.get(curIdx);
        // curAccount.remove(0);
        for(String email:curAccount){
            for(int i=0;i acc = accounts.get(i);
                if(acc.contains(email)&&email.contains("@")){
                    set.add(i);
                    set = getIdx(i,set,accounts);
                }
            }
        }
        return set;
    }
}

并查集版:

class Solution {
    public List> accountsMerge(List> accounts) {
        Map emailToIndex = new HashMap();
        Map emailToName = new HashMap();
        int emailsCount = 0;
        for(Listaccount:accounts){//遍历 写email的序号 和 email的name对
            String name = account.get(0);
            int size = account.size();
            for(int i=1;i account:accounts){ //把同一个人的邮箱合并到一个父节点下
            String firstEmail = account.get(1);
            int firstIndex = emailToIndex.get(firstEmail);
            int size = account.size();
            for(int i=2;i> indexToEmails = new HashMap>();
        for(String email:emailToIndex.keySet()){//把相同根节点的email放一起
            int index = uf.find(emailToIndex.get(email));
            List account = indexToEmails.getOrDefault(index, new ArrayList());
            account.add(email);
            indexToEmails.put(index, account);
        }

        List> merged = new ArrayList>();
        for(List emails:indexToEmails.values()){
            Collections.sort(emails);
            String name = emailToName.get(emails.get(0));
            List 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

你可能感兴趣的:(图论)