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;

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