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)
    }
}

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

时间: 2024-11-13 22:31:16

684. Redundant Connection的相关文章

LN : leetcode 684 Redundant Connection

lc 684 Redundant Connection 684 Redundant Connection In this problem, a tree is an undirected graph that is connected and has no cycles. The given input is a graph that started as a tree with N nodes (with distinct values 1, 2, ..., N), with one addi

[Swift]LeetCode684. 冗余连接 | Redundant Connection

In this problem, a tree is an undirected graph that is connected and has no cycles. The given input is a graph that started as a tree with N nodes (with distinct values 1, 2, ..., N), with one additional edge added. The added edge has two different v

LeetCode 685. Redundant Connection II

原题链接在这里:https://leetcode.com/problems/redundant-connection-ii/ 题目: In this problem, a rooted tree is a directed graph such that, there is exactly one node (the root) for which all other nodes are descendants of this node, plus every node has exactly

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

在本问题中, 树指的是一个连通且无环的无向图. 输入一个图,该图由一个有着N个节点 (节点值不重复1, 2, ..., N) 的树及一条附加的边构成.附加的边的两个顶点包含在1到N中间,这条附加的边不属于树中已存在的边. 结果图是一个以边组成的二维数组.每一个边的元素是一对[u, v] ,满足 u < v,表示连接顶点u 和v的无向图的边. 返回一条可以删去的边,使得结果图是一个有着N个节点的树.如果有多个答案,则返回二维数组中最后出现的边.答案边 [u, v] 应满足相同的格式 u < v.

LeetCode Problems List 题目汇总

No. Title Level Rate 1 Two Sum Medium 17.70% 2 Add Two Numbers Medium 21.10% 3 Longest Substring Without Repeating Characters Medium 20.60% 4 Median of Two Sorted Arrays Hard 17.40% 5 Longest Palindromic Substring Medium 20.70% 6 ZigZag Conversion Ea

Leetcode problems classified by company 题目按公司分类(Last updated: October 2, 2017)

Sorted by frequency of problems that appear in real interviews.Last updated: October 2, 2017Google (214)534 Design TinyURL388 Longest Absolute File Path683 K Empty Slots340 Longest Substring with At Most K Distinct Characters681 Next Closest Time482

UnionFind问题总结

UnionFind就是acm中常用的并查集... 常用操作 Ref 相关问题: Graph Valid Tree  (权限题做不了嘤嘤嘤) 684. Redundant Connection 685. Redundant Connection II 原文地址:https://www.cnblogs.com/pdev/p/9536921.html

【LeetCode】树(共94题)

[94]Binary Tree Inorder Traversal [95]Unique Binary Search Trees II (2018年11月14日,算法群) 给了一个 n,返回结点是 1 - n 的所有形态的BST. 题解:枚举每个根节点 r, 然后递归的生成左右子树的所有集合,然后做笛卡尔积. 1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *

【LeetCode】并查集 union-find(共16题)

链接:https://leetcode.com/tag/union-find/ p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [128]Longest Consecutive Sequence  (2018年11月22日,开始解决hard题) 给了一个无序的数组,问这个数组里面的元素(可以重新排序)能组成的最长的连续子序列是多长.本题的时间复杂度要求是 O(N). 本题 array 专题里面有, 链接:https