国产xxxx99真实实拍_久久不雅视频_高清韩国a级特黄毛片_嗯老师别我我受不了了小说

資訊專欄INFORMATION COLUMN

[LeetCode] 721. Accounts Merge

lk20150415 / 452人閱讀

Problem

Given a list accounts, each element accounts[i] is a list of strings, where the first element accountsi is a name, and the rest of the elements are emails representing emails of the account.

Now, we would like to merge these accounts. Two accounts definitely belong to the same person if there is some email that is common to both accounts. Note that even if two accounts have the same name, they may belong to different people as people could have the same name. A person can have any number of accounts initially, but all of their accounts definitely have the same name.

After merging the accounts, return the accounts in the following format: the first element of each account is the name, and the rest of the elements are emails in sorted order. The accounts themselves can be returned in any order.

Example 1:
Input:
accounts = [["John", "johnsmith@mail.com", "john00@mail.com"], ["John", "johnnybravo@mail.com"], ["John", "johnsmith@mail.com", "john_newyork@mail.com"], ["Mary", "mary@mail.com"]]
Output: [["John", "john00@mail.com", "john_newyork@mail.com", "johnsmith@mail.com"], ["John", "johnnybravo@mail.com"], ["Mary", "mary@mail.com"]]
Explanation:
The first and third John"s are the same person as they have the common email "johnsmith@mail.com".
The second John and Mary are different people as none of their email addresses are used by other accounts.
We could return these lists in any order, for example the answer [["Mary", "mary@mail.com"], ["John", "johnnybravo@mail.com"],
["John", "john00@mail.com", "john_newyork@mail.com", "johnsmith@mail.com"]] would still be accepted.
Note:

The length of accounts will be in the range [1, 1000].
The length of accounts[i] will be in the range [1, 10].
The length of accountsi will be in the range [1, 30].

Solution
class Solution {
    public List> accountsMerge(List> accounts) {
        //construct email graph
        Map parents = new HashMap<>();
        //save email-user relationships
        Map users = new HashMap<>();
        
        //initialize email graph, and save email-user map, btw
        for (List account: accounts) {
            for (int i = 1; i < account.size(); i++) {
                parents.put(account.get(i), account.get(i));
                users.put(account.get(i), account.get(0));
            }
        }
        
        //find parent and union to the first node"s parent in each group
        for (List account: accounts) {
            String parent = find(parents, account.get(1));
            for (int i = 2; i < account.size(); i++) {
                String curParent = find(parents, account.get(i));
                parents.put(curParent, parent);
            }
        }
        
        //loop through groups and save the unions
        Map> unions = new HashMap<>();
        for (List account: accounts) {
            String parent = find(parents, account.get(1));
            if (!unions.containsKey(parent)) {
                unions.put(parent, new TreeSet<>());
            }
            for (int i = 1; i < account.size(); i++) {
                unions.get(parent).add(account.get(i));
            }
        }
        
        //loop through unions map and save to result
        List> res = new ArrayList<>();
        for (String parent: unions.keySet()) {
            List emails = new ArrayList<>(unions.get(parent));
            emails.add(0, users.get(parent)); //put user name at start pos
            res.add(emails);
        }
        
        return res;
    }
    
    private String find(Map parents, String str) {
        if (!parents.containsKey(str)) return null;
        String parent = parents.get(str);
        if (parent.equals(str)) return parent;
        else return find(parents, parent);
    }
}

文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。

轉載請注明本文地址:http://specialneedsforspecialkids.com/yun/72358.html

相關文章

  • 從零構建、部署去中心化Voting Dapp

    摘要:查看文件內容這個文件主要是封裝了一個的供我們直接使用,可以從直接獲取到對象供文件中使用。 如何編寫智能合約(Smart Contract)- 從零構建和部署去中心化投票App,decentralization Voting Dapp 孔壹學院:國內區塊鏈職業教育領先品牌 作者:黎躍春,區塊鏈、高可用架構工程師微信:liyc1215 QQ群:348924182 博客:http://...

    mikasa 評論0 收藏0
  • [Leetcode] Merge Two Sorted Lists Merge K Sorted L

    摘要:注意因為堆中是鏈表節點,我們在初始化堆時還要新建一個的類。代碼初始化大小為的堆拿出堆頂元素將堆頂元素的下一個加入堆中 Merge Two Sorted Lists 最新更新請見:https://yanjia.me/zh/2019/01/... Merge two sorted linked lists and return it as a new list. The new list...

    stefanieliang 評論0 收藏0
  • [LeetCode/LintCode] Merge Intervals

    摘要:方法上沒太多難點,先按所有區間的起點排序,然后用和兩個指針,如果有交集進行操作,否則向后移動。由于要求的,就對原數組直接進行操作了。時間復雜度是的時間。 Problem Given a collection of intervals, merge all overlapping intervals. Example Given intervals => merged intervals...

    gougoujiang 評論0 收藏0
  • [LintCode/LeetCode] Merge Sorted Array

    Problem Given two sorted integer arrays A and B, merge B into A as one sorted array. Notice You may assume that A has enough space (size that is greater or equal to m + n) to hold additional elements ...

    summerpxy 評論0 收藏0
  • [Leetcode] Merge Sorted Array 合并數組

    摘要:但是如果我們從后往前,合并到第一個數組的最后,則不用位移。注意將和都先減,用和來代表下標,避免兩個數組為空時拋出空指針異常。 Merge Sorted Array 最新更新請見:https://yanjia.me/zh/2019/02/... Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1...

    quietin 評論0 收藏0

發表評論

0條評論

最新活動
閱讀需要支付1元查看
<