-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCalcEquation.java
More file actions
79 lines (67 loc) · 2.49 KB
/
CalcEquation.java
File metadata and controls
79 lines (67 loc) · 2.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import java.util.*;
/**
* @description:
* @Author: JachinDo
* @Date: 2019/11/07 19:05
*/
public class CalcEquation {
public double[] calcEquation(List<List<String>> equations, double[] values, List<List<String>> queries) {
double[][] graph = new double[26][26]; // 给每一个字母对应一个标号,来建立一张图
for (int i = 0; i < 26; i++) {
for (int j = 0; j < 26; j++) {
graph[i][j] = Double.MAX_VALUE;
}
}
HashMap<String,Integer> map = new HashMap<>(); // 其中字母与标号的映射用一个map实现
int index = 0;
for (int i = 0; i < equations.size(); i++) {
String A = equations.get(i).get(0);
String B = equations.get(i).get(1);
if (!map.containsKey(A)) {
map.put(A, index++);
}
if (!map.containsKey(B)) {
map.put(B, index++);
}
graph[map.get(A)][map.get(B)] = values[i];
if (values[i] != 0) {
graph[map.get(B)][map.get(A)] = 1 / values[i];
}
}
double[] res = new double[queries.size()];
for (int i = 0; i < queries.size(); i++) {
String A = queries.get(i).get(0);
String B = queries.get(i).get(1);
if (map.containsKey(A) && map.containsKey(B)) {
int src = map.get(A);
int dest = map.get(B);
double weight = 1.0;
HashSet<Integer> visited = new HashSet<>();
res[i] = dfs(src,dest,graph,weight,visited);
} else {
res[i] = -1.0;
}
}
return res;
}
public double dfs(int src, int dest, double[][] graph, double weight, HashSet<Integer> lastVisited) {
// visited 避免环路
// 因为java传递set时为引用,所以要在内部重新处理
HashSet<Integer> visited = new HashSet<>(lastVisited);
visited.add(src);
if (src == dest) {
return weight;
}
for (int i = 0; i < 26; i++) {
double temp = Double.MIN_VALUE;
if (src != i && graph[src][i] != Double.MAX_VALUE && (!visited.contains(i))) {
visited.add(i);
temp = dfs(i, dest, graph, weight * graph[src][i], visited);
if (temp != Double.MIN_VALUE && temp != (-1.0)) {
return temp;
}
}
}
return -1.0;
}
}