首页 > 其他 > 详细

684. Redundant Connection

时间:2020-02-15 09:06:40      阅读:45      评论:0      收藏:0      [点我收藏+]

此题可以使用两种思路来解决:

  • DFS
  • Union-Find

 以下是使用上一篇的数据结构Union-Find来处理的代码:

/**
 * LeetCode_146
 * https://leetcode.com/problems/redundant-connection/description/
 * https://www.youtube.com/watch?v=4hJ721ce010&list=LLaIZDn4w2rZnhRNMRMelhfg
 * */
class Solution {
    fun findRedundantConnection(edges: Array<IntArray>): IntArray {
        val size = edges.size
        val unionFindSet = UnionFindSet(size)
        for (edge in edges) {
            //there are 2 nodes in every edge
            //if they are have same parent, union() return false, so just return this edge
            if (!unionFindSet.union(edge[0], edge[1])) {
                return edge
            }
        }
        return IntArray(1)
    }
}

 

684. Redundant Connection

原文:https://www.cnblogs.com/johnnyzhao/p/12310461.html

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