Skip to content

Commit f313a52

Browse files
committed
fd
1 parent b8283ac commit f313a52

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -372,4 +372,5 @@
372372
|560|[Subarray Sum Equals K](https://leetcode.com/problems/subarray-sum-equals-k/)| [Java](https://github.com/dingjikerbo/leetcode/blob/master/solution/src/main/java/com/inuker/solution/SubarraySumEqualsK.java)|70||
373373
|637|[Average of Levels in Binary Tree](https://leetcode.com/problems/average-of-levels-in-binary-tree/#/description)| [Java](https://github.com/dingjikerbo/leetcode/blob/master/solution/src/main/java/com/inuker/solution/AverageOfLevelsInBinaryTree.java)|100||
374374
|652|[Find Duplicate Subtrees](https://leetcode.com/problems/find-duplicate-subtrees/)| [Java](https://github.com/dingjikerbo/leetcode/blob/master/solution/src/main/java/com/inuker/solution/FindDuplicateSubtrees.java)|70|开始还没思路|
375-
|653|[Two Sum IV - Input is a BST](https://leetcode.com/problems/two-sum-iv-input-is-a-bst/)| [Java](https://github.com/dingjikerbo/leetcode/blob/master/solution/src/main/java/com/inuker/solution/TwoSumIV.java)|90||
375+
|653|[Two Sum IV - Input is a BST](https://leetcode.com/problems/two-sum-iv-input-is-a-bst/)| [Java](https://github.com/dingjikerbo/leetcode/blob/master/solution/src/main/java/com/inuker/solution/TwoSumIV.java)|90||
376+
|684|[Redundant Connection](https://leetcode.com/problems/redundant-connection/)| [Java](https://github.com/dingjikerbo/leetcode/blob/master/solution/src/main/java/com/inuker/solution/RedundantConnection.java)|||
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.inuker.solution;
2+
3+
/**
4+
* Created by liwentian on 2017/12/7.
5+
*/
6+
7+
/**
8+
* https://leetcode.com/articles/redundant-connection/
9+
*/
10+
public class RedundantConnection {
11+
/**
12+
* 思路很简单,发现第一个联通的边时就是多余的
13+
*/
14+
public int[] findRedundantConnection(int[][] edges) {
15+
int[] arr = new int[2001];
16+
for (int i = 0; i < arr.length; i++) {
17+
arr[i] = i;
18+
}
19+
for (int[] edge : edges) {
20+
int from = edge[0], to = edge[1];
21+
int from0 = find(arr, from), to0 = find(arr, to);
22+
if (from0 == to0) {
23+
return edge;
24+
}
25+
arr[from0] = to0;
26+
}
27+
return new int[2];
28+
}
29+
30+
private int find(int[] nums, int i) {
31+
for ( ; nums[i] != i; i = nums[i]);
32+
return i;
33+
}
34+
}

0 commit comments

Comments
 (0)