LeetCode721。アカウントの統合(複合検索)

記事ディレクトリ

1.タイトル

アカウントのリストを考えると、各要素accounts[i]は文字列のリストであり、最初の要素accounts[i][0]は(name)の名前であり、残りの要素はアカウントの電子メールアドレスを示す電子メールです。

次に、これらのアカウントをマージします。
2つのアカウントに共通の電子メールアドレスがある場合、2つのアカウントは同じ人物に属している必要があります。
2つのアカウントが同じ名前であっても、同じ名前を持つ可能性があるため、異なるアカウントに属している場合があります。
人は最初はいくつでもアカウントを持つことができますが、すべてのアカウントは同じ名前を持っています。

アカウントをマージすると、アカウントは次の形式で返されます。各アカウントの最初の要素は名前で、残りの要素は順番に電子メールアドレスです。
アカウント自体は任意の順序で返すことができます。

例子 1:
Input: 
accounts = [["John", "[email protected]", "[email protected]"], 
["John", "[email protected]"], 
["John", "[email protected]", "[email protected]"], 
["Mary", "[email protected]"]]

Output: [["John", '[email protected]', '[email protected]', '[email protected]'], 
 ["John", "[email protected]"], 
 ["Mary", "[email protected]"]]
 
Explanation: 
  第一个和第三个 John 是同一个人,因为他们有共同的电子邮件 "[email protected]"。 
  第二个 John 和 Mary 是不同的人,因为他们的电子邮件地址没有被其他帐户使用。
  我们可以以任何顺序返回这些列表,
  例如答案[['Mary''[email protected]']['John''[email protected]']['John''[email protected]''[email protected]''[email protected]']]
  仍然会被接受。

注意:
accounts的长度将在[11000]的范围内。
accounts[i]的长度将在[110]的范围内。
accounts[i][j]的长度将在[130]的范围内。

出典:LeetCodeリンク:https://leetcode-cn.com/problems/accounts-merge
著作権はLeetCode が所有しています商用転載については、正式な許可書にご連絡ください。非商用転載については、出典を明記してください。

2.問題解決

class dsu
{
    
    
public:
    unordered_map<string,string> f;
    dsu(vector<vector<string>>& accounts)
    {
    
    
        for(int i = 0; i < accounts.size(); ++i)
        {
    
    
            for(int j = 1; j < accounts[i].size(); ++j)
            {
    
    
                if(!f.count(accounts[i][j]))
                    f[accounts[i][j]] = accounts[i][1];
                        //以第一个邮件作为代表
                else
                    merge(accounts[i][j], accounts[i][1]);
                    //有邮件出现过,合并该邮件和当前邮件代表
            }
        }
    }
    void merge(string& a, string& b)
    {
    
    
        string fa = find(a), fb = find(b);
        f[fa] = fb;
    }
    string find(string &a)
    {
    
    
        if(f[a] == a)
            return a;
        return f[a] = find(f[a]);
    }
};
class Solution {
    
    
public:
    vector<vector<string>> accountsMerge(vector<vector<string>>& accounts) {
    
    
        unordered_map<string, string> m;
        for(int i = 0; i < accounts.size(); ++i)
        {
    
    
            m[accounts[i][1]] = accounts[i][0];
            //邮件代表,归属的人名
        }
        dsu u(accounts);
        unordered_map<string, vector<string>> ans;
        string mail, name, tpmail;
        for(auto it = u.f.begin(); it != u.f.end(); ++it)
        {
    
       
            mail = it->first;
            tpmail = u.find(mail);//邮件代表tpmail
            ans[tpmail].push_back(mail);
        }
        vector<vector<string>> res(ans.size());
        int i = 0;
        for(auto it = ans.begin(); it != ans.end(); ++it, i++)
        {
    
    
            res[i] = it->second;//邮件列表
            sort(res[i].begin(), res[i].end());//排序
            res[i].insert(res[i].begin(), m[it->first]);//插入人名
        }
        return res;
    }
};

288 ms 26.9 MB


私のCSDN ブログアドレスhttps://michael.blog.csdn.net/

QRコードを長押しまたはスキャンして、私の公式アカウント(Michael Amin)をフォローし、一緒に来て、学び、一緒に進歩してください!
マイケル・アミン

おすすめ

転載: blog.csdn.net/qq_21201267/article/details/108704420