Skip to content

Commit ef093a9

Browse files
refactor 399
1 parent 0459666 commit ef093a9

File tree

1 file changed

+54
-49
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+54
-49
lines changed

src/main/java/com/fishercoder/solutions/_399.java

Lines changed: 54 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -30,62 +30,67 @@
3030
*/
3131
public class _399 {
3232

33-
/**Credit: https://discuss.leetcode.com/topic/59146/java-ac-solution-using-graph
34-
*
35-
* Image a/b = k as a link between node a and b, the weight from a to b is k, the reverse link is 1/k. Query is to find a path between two nodes.*/
36-
public double[] calcEquation(String[][] equations, double[] values, String[][] queries) {
37-
Map<String, List<String>> pairs = new HashMap<>();
38-
Map<String, List<Double>> valuePairs = new HashMap<>();
39-
for (int i = 0; i < equations.length; i++) {
40-
String[] equation = equations[i];
41-
if (!pairs.containsKey(equation[0])) {
42-
pairs.put(equation[0], new ArrayList<>());
43-
valuePairs.put(equation[0], new ArrayList<>());
33+
public static class Solution1 {
34+
/**
35+
* Credit: https://discuss.leetcode.com/topic/59146/java-ac-solution-using-graph
36+
*
37+
* Image a/b = k as a link between node a and b, the weight from a to b is k, the reverse link
38+
* is 1/k. Query is to find a path between two nodes.
39+
*/
40+
public double[] calcEquation(String[][] equations, double[] values, String[][] queries) {
41+
Map<String, List<String>> pairs = new HashMap<>();
42+
Map<String, List<Double>> valuePairs = new HashMap<>();
43+
for (int i = 0; i < equations.length; i++) {
44+
String[] equation = equations[i];
45+
if (!pairs.containsKey(equation[0])) {
46+
pairs.put(equation[0], new ArrayList<>());
47+
valuePairs.put(equation[0], new ArrayList<>());
48+
}
49+
if (!pairs.containsKey(equation[1])) {
50+
pairs.put(equation[1], new ArrayList<>());
51+
valuePairs.put(equation[1], new ArrayList<>());
52+
}
53+
pairs.get(equation[0]).add(equation[1]);
54+
pairs.get(equation[1]).add(equation[0]);
55+
valuePairs.get(equation[0]).add(values[i]);
56+
valuePairs.get(equation[1]).add(1 / values[i]);
4457
}
45-
if (!pairs.containsKey(equation[1])) {
46-
pairs.put(equation[1], new ArrayList<>());
47-
valuePairs.put(equation[1], new ArrayList<>());
48-
}
49-
pairs.get(equation[0]).add(equation[1]);
50-
pairs.get(equation[1]).add(equation[0]);
51-
valuePairs.get(equation[0]).add(values[i]);
52-
valuePairs.get(equation[1]).add(1 / values[i]);
53-
}
5458

55-
double[] result = new double[queries.length];
56-
for (int i = 0; i < queries.length; i++) {
57-
String[] query = queries[i];
58-
result[i] = dfs(query[0], query[1], pairs, valuePairs, new HashSet<>(), 1.0);
59-
if (result[i] == 0.0) {
60-
result[i] = -1.0;
59+
double[] result = new double[queries.length];
60+
for (int i = 0; i < queries.length; i++) {
61+
String[] query = queries[i];
62+
result[i] = dfs(query[0], query[1], pairs, valuePairs, new HashSet<>(), 1.0);
63+
if (result[i] == 0.0) {
64+
result[i] = -1.0;
65+
}
6166
}
67+
return result;
6268
}
63-
return result;
64-
}
6569

66-
private double dfs(String start, String end, Map<String, List<String>> pairs, Map<String, List<Double>> valuePairs, HashSet<String> set, double value) {
67-
if (set.contains(start)) {
68-
return 0.0;
69-
}
70-
if (!pairs.containsKey(start)) {
71-
return 0.0;
72-
}
73-
if (start.equals(end)) {
74-
return value;
75-
}
76-
set.add(start);
70+
private double dfs(String start, String end, Map<String, List<String>> pairs,
71+
Map<String, List<Double>> valuePairs, HashSet<String> set, double value) {
72+
if (set.contains(start)) {
73+
return 0.0;
74+
}
75+
if (!pairs.containsKey(start)) {
76+
return 0.0;
77+
}
78+
if (start.equals(end)) {
79+
return value;
80+
}
81+
set.add(start);
7782

78-
List<String> stringList = pairs.get(start);
79-
List<Double> valueList = valuePairs.get(start);
80-
double tmp = 0.0;
81-
for (int i = 0; i < stringList.size(); i++) {
82-
tmp = dfs(stringList.get(i), end, pairs, valuePairs, set, value * valueList.get(i));
83-
if (tmp != 0.0) {
84-
break;
83+
List<String> stringList = pairs.get(start);
84+
List<Double> valueList = valuePairs.get(start);
85+
double tmp = 0.0;
86+
for (int i = 0; i < stringList.size(); i++) {
87+
tmp = dfs(stringList.get(i), end, pairs, valuePairs, set, value * valueList.get(i));
88+
if (tmp != 0.0) {
89+
break;
90+
}
8591
}
92+
set.remove(start);
93+
return tmp;
8694
}
87-
set.remove(start);
88-
return tmp;
8995
}
90-
9196
}

0 commit comments

Comments
 (0)