zoukankan      html  css  js  c++  java
  • 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)
        }
    }
  • 相关阅读:
    nginx安装:linux环境下安装包安装
    Effective C++
    Win32 Msg
    C++ 17
    Win32 Threads
    Exceptional C++
    C++11
    STL 分类
    C++类型转换
    红黑树
  • 原文地址:https://www.cnblogs.com/johnnyzhao/p/12310461.html
Copyright © 2011-2022 走看看