每日一题 账户合并 LeetCode721

69 篇文章 0 订阅
44 篇文章 0 订阅

这个月LeetCode真的猛出并查集,学不会并查集谁也别想走。
并查集方法最重要的方法就是对问题进行建模,使得问题用连通分量表示,并且转化为判断两点是否连通的问题。然后要思考在构建并查集的过程中要如何构建并查集。

哈希表加并查集

需要两个哈希表,分别将邮箱映射成index和映射到对应的用户的名字。
把每个用户对应的邮箱地址看成是一个连通分量。构建连通分量时只需要把accounts数组中每个用户对应的邮箱地址合并起来就好。

// 并查集
class UnionFind {
public:
    UnionFind(int n): f(n) { // 初始化
        for (int i = 0; i < n; ++i) {
            f[i] = i;
        }
    }

    int findf(int x) { // 寻找一个集合的代表结点,并进行路径压缩
        if (f[x] != x) {
            f[x] = findf(f[x]);
        }
        return f[x];
    }

    void merge(int x, int y) { // 如果两个点位于不同的连通分量,则合并这两个连通分量
        int fx = findf(x), fy = findf(y);
        if (fx == fy) return;
        f[fx] = fy;
    }

private:
    vector<int> f;
};

class Solution {
public:
    vector<vector<string>> accountsMerge(vector<vector<string>>& accounts) {
        unordered_map<string, int> emailtoIndex; // 邮箱到索引
        unordered_map<string, string> emailtoName; // 邮箱到所属用户名
        int accountsNums = accounts.size();
        int emailscounts = 0; // 用来将邮箱映射到index;

        // 构建邮箱到数字索引和邮箱到用户名的哈希表
        for (int i = 0; i < accountsNums; ++i) {
            int emailNums = accounts[i].size();
            for (int j = 1; j < emailNums; ++j) {
                if (emailtoIndex.find(accounts[i][j]) == emailtoIndex.end()) {
                    emailtoIndex[accounts[i][j]] = emailscounts++;
                    emailtoName[accounts[i][j]] = accounts[i][0];
                }
            }
        }

        // 构建并查集,一个人的所有邮箱相当于一个连通分量,遍历每个accounts,然后在并查集里合并它们的邮箱
        UnionFind unionFind(emailscounts);
        for (int i = 0; i < accountsNums; ++i) {
            int emailNums = accounts[i].size();
            for (int j = 2; j < emailNums; ++j) {
                unionFind.merge(emailtoIndex[accounts[i][1]], emailtoIndex[accounts[i][j]]);
            }
        }

        // 构建答案
        map<int, vector<string>> indextoEmails; // 表示出邮箱的连通分量,key值是每个连通分量的代表元素,每个连通分量就表示了一个用户
        // 先要找到每个连通分量的代表结点,然后通过代表结点
        for (auto& [email, i] : emailtoIndex) {
            int index = unionFind.findf(i);
            indextoEmails[index].emplace_back(email);
        }

        vector<vector<string>> result;
        for (auto& [_, emails] : indextoEmails) {
            sort(emails.begin(), emails.end()); // 根据题目要求按照字典序排序
            string& name = emailtoName[emails[0]]; // 根据一个连通分量里任意一个邮箱地址确定出用户名称
            vector<string> account;
            account.emplace_back(name);
            for (auto& email : emails) {
                account.emplace_back(email);
            }
            result.emplace_back(account);
        }

        return result;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值