Skip to content

Commit 547c75d

Browse files
committed
fd
1 parent f313a52 commit 547c75d

File tree

3 files changed

+30
-40
lines changed

3 files changed

+30
-40
lines changed

solution/src/main/java/com/inuker/solution/NumberOfConnectedComponents.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package com.inuker.solution;
22

33
import java.util.ArrayList;
4+
import java.util.HashSet;
45
import java.util.List;
6+
import java.util.Set;
57

68
/**
79
* Created by dingjikerbo on 17/1/2.
@@ -35,7 +37,6 @@ private int find(int[] nums, int i) {
3537
/**
3638
* DFS法
3739
*/
38-
// 8ms
3940
public int countComponents2(int n, int[][] edges) {
4041
List<Integer>[] graph = new ArrayList[n];
4142
for (int i = 0; i < n; i++) {
@@ -45,22 +46,21 @@ public int countComponents2(int n, int[][] edges) {
4546
graph[edge[0]].add(edge[1]);
4647
graph[edge[1]].add(edge[0]);
4748
}
48-
boolean[] visited = new boolean[n];
49+
Set<Integer> visited = new HashSet<>();
4950
int count = 0;
5051
for (int i = 0; i < n; i++) {
51-
if (!visited[i]) {
52-
dfs(graph, i, visited);
52+
if (!visited.contains(i)) {
5353
count++;
54+
dfs(graph, i, visited);
5455
}
5556
}
5657
return count;
5758
}
5859

59-
private void dfs(List<Integer>[] graph, int i, boolean[] visited) {
60-
if (visited[i]) {
60+
private void dfs(List<Integer>[] graph, int i, Set<Integer> visited) {
61+
if (!visited.add(i)) {
6162
return;
6263
}
63-
visited[i] = true;
6464
for (Integer k : graph[i]) {
6565
dfs(graph, k, visited);
6666
}

solution/src/main/java/com/inuker/solution/RedundantConnection.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
public class RedundantConnection {
1111
/**
1212
* 思路很简单,发现第一个联通的边时就是多余的
13+
* 时间复杂度O(n),空间O(n)
1314
*/
1415
public int[] findRedundantConnection(int[][] edges) {
1516
int[] arr = new int[2001];

test/src/main/java/com/inuker/test/Test2.java

Lines changed: 22 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import java.util.Map;
1212
import java.util.PriorityQueue;
1313
import java.util.Queue;
14+
import java.util.Set;
1415
import java.util.TreeMap;
1516

1617
/**
@@ -19,44 +20,32 @@
1920

2021
public class Test2 {
2122

22-
public double[] calcEquation(String[][] equations, double[] values, String[][] queries) {
23-
HashMap<String, HashMap<String, Double>> valueMap = new HashMap<>();
24-
25-
for (int i = 0; i < equations.length; i++) {
26-
String[] equation = equations[i];
27-
HashMap<String, Double> map = valueMap.computeIfAbsent(equation[0], k -> new HashMap<>());
28-
map.put(equation[1], values[i]);
29-
map = valueMap.computeIfAbsent(equation[1], k -> new HashMap<>());
30-
map.put(equation[0], 1 / values[i]);
23+
public int countComponents2(int n, int[][] edges) {
24+
List<Integer>[] graph = new ArrayList[n];
25+
for (int i = 0; i < n; i++) {
26+
graph[i] = new ArrayList<>();
3127
}
32-
33-
double[] result = new double[queries.length];
34-
for (int i = 0; i < queries.length; i++) {
35-
double[] value = new double[] {1.0};
36-
result[i] = dfs(valueMap, queries[i][0], queries[i][1], new HashSet<>(), value) ? value[0] : -1.0;
28+
for (int[] edge : edges) {
29+
graph[edge[0]].add(edge[1]);
30+
graph[edge[1]].add(edge[0]);
31+
}
32+
Set<Integer> visited = new HashSet<>();
33+
int count = 0;
34+
for (int i = 0; i < n; i++) {
35+
if (!visited.contains(i)) {
36+
count++;
37+
dfs(graph, i, visited);
38+
}
3739
}
38-
return result;
40+
return count;
3941
}
4042

41-
private boolean dfs(HashMap<String, HashMap<String, Double>> map, String start, String end, HashSet<String> set, double[] value) {
42-
if (!map.containsKey(start) || !map.containsKey(end) || set.contains(start)) {
43-
return false;
44-
}
45-
if (start.equals(end)) {
46-
return true;
43+
private void dfs(List<Integer>[] graph, int i, Set<Integer> visited) {
44+
if (!visited.add(i)) {
45+
return;
4746
}
48-
set.add(start);
49-
HashMap<String, Double> valueMap = map.get(start);
50-
boolean flag = false;
51-
for (Map.Entry<String, Double> entry : valueMap.entrySet()) {
52-
value[0] *= entry.getValue();
53-
if (dfs(map, entry.getKey(), end, set, value)) {
54-
flag = true;
55-
break;
56-
}
57-
value[0] /= entry.getValue();
47+
for (Integer k : graph[i]) {
48+
dfs(graph, k, visited);
5849
}
59-
set.remove(start);
60-
return flag;
6150
}
6251
}

0 commit comments

Comments
 (0)