684. Redundant Connection

Posted johnnyzhao

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了684. Redundant Connection相关的知识,希望对你有一定的参考价值。

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

  • 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的主要内容,如果未能解决你的问题,请参考以下文章

684. Redundant Connection

684. Redundant Connection

684. Redundant Connection

Graph-684. Redundant Connection

[LeetCode] 684. Redundant Connection 冗余的连接

Leetcode之并查集专题-684. 冗余连接(Redundant Connection)