首页 > 其他 > 详细

684. Redundant Connection

时间:2018-05-04 16:01:34      阅读:150      评论:0      收藏:0      [点我收藏+]

https://leetcode.com/problems/redundant-connection/description/

Use map to do Union Find.

class Solution {
public:
    vector<int> findRedundantConnection(vector<vector<int>>& edges) {
        unordered_map<int,int> parent;
        for (const auto& e : edges) {
            int p = findParent(parent, e[0]);
            int c = findParent(parent, e[1]);
            if (p == c)
                return e;
            parent[c] = p;
        }
        return vector<int>();
    }
    int findParent(unordered_map<int,int>& parents, int child) {
        while (parents.count(child))
            child = parents[child];
        return child;
    }
};

 

684. Redundant Connection

原文:https://www.cnblogs.com/JTechRoad/p/8990947.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!