码迷,mamicode.com
首页 > 其他好文 > 详细

684. Redundant Connection

时间:2018-05-04 16:50:33      阅读:160      评论:0      收藏:0      [点我收藏+]

标签:red   public   rip   ==   nec   order   desc   http   union   

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

标签:red   public   rip   ==   nec   order   desc   http   union   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!