diff --git a/src/main/java/leetcode/editor/cn/RedundantConnection.java b/src/main/java/leetcode/editor/cn/RedundantConnection.java new file mode 100644 index 0000000..e1650eb --- /dev/null +++ b/src/main/java/leetcode/editor/cn/RedundantConnection.java @@ -0,0 +1,112 @@ +//

树可以看成是一个连通且 无环 的 无向 图。

+// +//

给定往一棵 n 个节点 (节点值 1~n) 的树中添加一条边后的图。添加的边的两个顶点包含在 1n 中间,且这条附加的边不属于树中已存在的边。图的信息记录于长度为 n 的二维数组 edges ,edges[i] = [ai, bi] 表示图中在 aibi 之间存在一条边。

+// +//

请找出一条可以删去的边,删除后可使得剩余部分是一个有着 n 个节点的树。如果有多个答案,则返回数组 edges 中最后出现的边。

+// +//

 

+// +//

示例 1:

+// +//

+// +//
+//输入: edges = [[1,2], [1,3], [2,3]]
+//输出: [2,3]
+//
+// +//

示例 2:

+// +//

+// +//
+//输入: edges = [[1,2], [2,3], [3,4], [1,4], [1,5]]
+//输出: [1,4]
+//
+// +//

 

+// +//

提示:

+// +// +// +//
Related Topics
  • 深度优先搜索
  • 广度优先搜索
  • 并查集

  • 👍 523
  • 👎 0
  • +package leetcode.editor.cn; + +import java.util.*; + +// 684:冗余连接 +public class RedundantConnection { + public static void main(String[] args) { + Solution solution = new RedundantConnection().new Solution(); + // TO TEST + } + + //leetcode submit region begin(Prohibit modification and deletion) + class Solution { + public int[] findRedundantConnection(int[][] edges) { + List> list = new ArrayList<>(); + for (int[] edge : edges) { + if (list.size() == 0) { + list.add(new ArrayList<>(Arrays.asList(edge[0], edge[1]))); + } else { + int index0 = -1; + int index1 = -1; + for (int i = list.size() - 1; i >= 0; i--) { + if (list.get(i).contains(edge[0])) { + index0 = i; + if (index1 > -1) { + break; + } + } + if (list.get(i).contains(edge[1])) { + index1 = i; + if (index0 > -1) { + break; + } + } + } + if (index0 > -1 && index0 == index1) { + return edge; + } + Set tmp = new HashSet<>(); + if (index0 > -1) { + tmp.addAll(list.get(index0)); + } else { + tmp.add(edge[0]); + } + if (index1 > -1) { + tmp.addAll(list.get(index1)); + } else { + tmp.add(edge[1]); + } + if (index0 > -1 && index1 > -1) { + if (index0 > index1) { + list.remove(index0); + list.remove(index1); + } else { + list.remove(index1); + list.remove(index0); + } + } else if (index0 > -1) { + list.remove(index0); + } else if (index1 > -1) { + list.remove(index1); + } + list.add(new ArrayList<>(tmp)); + } + } + return null; + } + } +//leetcode submit region end(Prohibit modification and deletion) + +} diff --git a/src/main/java/leetcode/editor/cn/doc/content/RedundantConnection.md b/src/main/java/leetcode/editor/cn/doc/content/RedundantConnection.md new file mode 100644 index 0000000..40f5c48 --- /dev/null +++ b/src/main/java/leetcode/editor/cn/doc/content/RedundantConnection.md @@ -0,0 +1,41 @@ +

    树可以看成是一个连通且 无环 的 无向 图。

    + +

    给定往一棵 n 个节点 (节点值 1~n) 的树中添加一条边后的图。添加的边的两个顶点包含在 1n 中间,且这条附加的边不属于树中已存在的边。图的信息记录于长度为 n 的二维数组 edges ,edges[i] = [ai, bi] 表示图中在 aibi 之间存在一条边。

    + +

    请找出一条可以删去的边,删除后可使得剩余部分是一个有着 n 个节点的树。如果有多个答案,则返回数组 edges 中最后出现的边。

    + +

     

    + +

    示例 1:

    + +

    + +
    +输入: edges = [[1,2], [1,3], [2,3]]
    +输出: [2,3]
    +
    + +

    示例 2:

    + +

    + +
    +输入: edges = [[1,2], [2,3], [3,4], [1,4], [1,5]]
    +输出: [1,4]
    +
    + +

     

    + +

    提示:

    + + + +
    Related Topics
  • 深度优先搜索
  • 广度优先搜索
  • 并查集

  • 👍 523
  • 👎 0
  • \ No newline at end of file