LeetCode 721( Accounts Merge)

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

2.例子:

输入:
accounts = [[“John”, “[email protected]”, “[email protected]”], [“John”, “[email protected]”], [“John”, “[email protected]”, “[email protected]”], [“Mary”, “[email protected]”]]

输出: [[“John”, ‘[email protected]’, ‘[email protected]’, ‘[email protected]’], [“John”, “[email protected]”], [“Mary”, “[email protected]”]]

解释:名字叫 “John”的有重复的账号信息,把他们绑定到一起。结果为[“John”, ‘[email protected]’, ‘[email protected]’, ‘[email protected]’] 。
(是[“John”, “[email protected]”, “[email protected]”]与[“John”, “[email protected]”, “[email protected]”]的合并。)剩下的由于没有相重复的信息,所以原样加到结果的 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;

    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章