LeetCode 721( Accounts Merge)

本文介绍了一种用于合并具有相同名称的用户账户信息的算法。该算法接收一个包含多个账户信息的列表作为输入,并将属于同一用户的账户信息进行合并,同时确保邮箱地址按字典顺序排列。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.题目描述:
(1)给定一个ArrayList的 accounts, 每一个元素都为一个 ArrayList。包含了用户名字,紧接着是用户的账号信息。
(2)从给定的元素中找到同一个用户的账号并把它们按照字典排序放到一个 ArrayList中返回。

2.例子:

输入:
accounts = [[“John”, “johnsmith@mail.com”, “john00@mail.com”], [“John”, “johnnybravo@mail.com”], [“John”, “johnsmith@mail.com”, “john_newyork@mail.com”], [“Mary”, “mary@mail.com”]]

输出: [[“John”, ‘john00@mail.com’, ‘john_newyork@mail.com’, ‘johnsmith@mail.com’], [“John”, “johnnybravo@mail.com”], [“Mary”, “mary@mail.com”]]

解释:名字叫 “John”的有重复的账号信息,把他们绑定到一起。结果为[“John”, ‘john00@mail.com’, ‘john_newyork@mail.com’, ‘johnsmith@mail.com’] 。
(是[“John”, “johnsmith@mail.com”, “john00@mail.com”]与[“John”, “johnsmith@mail.com”, “john_newyork@mail.com”]的合并。)剩下的由于没有相重复的信息,所以原样加到结果的 ArrayList中。

3.代码:

class Solution {
    public List<List<String>> accountsMerge(List<List<String>> accounts) {
        int accountsLen = accounts.size();
        //不再检查已经加到 res中的ArrayList元素
        boolean [] checked = new boolean[accountsLen];
        //判断是否是有相同名字的 ArrayList元素,没有的话直接输出
        HashMap<String, Integer>  nameTimes = new HashMap<>();
        //结果
        List<List<String>> res = new ArrayList<>();
        //首先判断名字是否重复
        for(List<String> tempList : accounts){
            nameTimes.put(tempList.get(0), nameTimes.getOrDefault(tempList.get(0), 0)+1);
        }

        for(int i = 0; i < accountsLen; i++){
            if(!checked[i]){
                checked[i] = true;
                String name = accounts.get(i).get(0);
                List<String> accountsTemp = accounts.get(i);
                HashSet<String> accountsSet = new HashSet<>();
                for(int j = 1; j < accountsTemp.size(); j++){
                    accountsSet.add(accountsTemp.get(j));
                }
                //名字出现一次的直接输出
                if(nameTimes.get(name) == 1){
                    System.out.println("1");
                    List<String> tempList = new ArrayList<>(accountsSet);
                    //字典排序又想账户
                    Collections.sort(tempList);
                    List<String> subAccounts = new ArrayList<>();
                    subAccounts.add(name);
                    subAccounts.addAll(tempList);
                    res.add(subAccounts);
                }else{
                //遍历剩下的 ArrayList,找同名的。
                    for(int j = i+1; j < accountsLen; j++){
                        if(!checked[j]){
                            if(name.equals(accounts.get(j).get(0))){
                                for(String ss : accounts.get(j)){
                                    if(accountsSet.contains(ss)){
                                        checked[j] = true;
                                        for(int k = 1; k < accounts.get(j).size(); k++) accountsSet.add(accounts.get(j).get(k));
                      //如果找到从头遍历。从而便利出所有两两相重的情况。
                                        j = i;
                                        break;
                                    }
                                }
                            }
                        }
                    }
                    List<String> tempList = new ArrayList<>(accountsSet);
                    Collections.sort(tempList);
                    List<String> subAccounts = new ArrayList<>();
                    subAccounts.add(name);
                    subAccounts.addAll(tempList);
                    res.add(subAccounts);
                }
            }
        }

        return res;

    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值