LeetCode 721. 账户合并

LeetCode 721. 账户合并
记录下自己菜菜的日常

class Solution {
    public List<List<String>> accountsMerge(List<List<String>> accounts) {
        //用来记录每个 email 对应在 parent 数组中的索引
        Map<String, Integer> projection = new HashMap<>();
        //用来记录每个 email 对应的姓名
        Map<String, String> corresponding = new HashMap<>();
        //用来记录每个 parent 数组中的索引对应的 email
        Map<Integer, String> projection1 = new HashMap<>();
        List<List<String>> ans = new ArrayList<>();
        int count = 0;
        //遍历每个 email 并加入到上述的集合中
        for(List<String> account : accounts){
            for(int i = 1; i < account.size(); ++i){
                if(!projection.containsKey(account.get(i))){
                    projection.put(account.get(i), count);
                    projection1.put(count, account.get(i));
                    ++count;
                }
                if(!corresponding.containsKey(account.get(i))){
                    corresponding.put(account.get(i), account.get(0));
                }
            }
        }
        //创建 parent 数组
        int[] parent = new int[projection.size()];
        initParent(parent);

        for (List<String> account : accounts) {
            //获取当前集合中的第一个作为这个集合中的父结点
            String s = account.get(1);
            for (int i = 2; i < account.size(); i++) {
                merge(projection.get(s), projection.get(account.get(i)), parent);
            }
        }

        //用来判断当前这个父索引是否已经拥有结果集,若有,则值就是在 ans 中的索引
        Map<Integer, Integer> visited = new HashMap<>();
        for(int i = 0; i < parent.length; ++i){
            int parent1 = findParent(i, parent);
            if(visited.containsKey(parent1)){
                //如果含有当前父索引的结果集,则直接把当前 email 添加进去
                int index = visited.get(parent1);
                List<String> str = ans.get(index);
                str.add(projection1.get(i));
            }else{
                //不包含,则新建结果集合,进行添加
                String s = projection1.get(i);
                List<String> list = new ArrayList<>();
                list.add(corresponding.get(s));
                list.add(s);
                ans.add(list);
                //当前新建的结果集在 ans 中的索引与 当前索引的父索引绑定
                visited.put(parent1, ans.size()-1);
            }
        }

        //对 ans 中的每一个集合进行字典序排序
        for (List<String> an : ans) {
            Collections.sort(an);
        }

        return ans;
    }
    //初始化数组
    public void initParent(int[] parent){
        for(int i = 0; i < parent.length; ++i){
            parent[i] = -1;
        }
    }
    //寻找当前索引的父索引
    public int findParent(int i, int[] parent){
        while(parent[i] != -1){
            i = parent[i];
        }
        return i;
    }
    //当两个索引不含有同一个父索引就进行合并
    public void merge(int i, int j, int[] parent){
        int iParent = findParent(i, parent);
        int jParent = findParent(j, parent);
        if(iParent == jParent) return;
        parent[iParent] = jParent;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值