Skip to content

Commit 4ac74b9

Browse files
committed
fd
1 parent d0758f7 commit 4ac74b9

File tree

2 files changed

+106
-36
lines changed

2 files changed

+106
-36
lines changed

leetcode/solution/src/EvaluateDivision.java

Lines changed: 42 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,43 +9,59 @@
99
public class EvaluateDivision {
1010

1111
public double[] calcEquation(String[][] equations, double[] values, String[][] queries) {
12-
HashMap<String, HashMap<String, Double>> valueMap = new HashMap<>();
13-
12+
HashMap<String, HashMap<String, Double>> table = new HashMap<>();
1413
for (int i = 0; i < equations.length; i++) {
15-
String[] equation = equations[i];
16-
HashMap<String, Double> map = valueMap.computeIfAbsent(equation[0], k -> new HashMap<>());
17-
map.put(equation[1], values[i]);
18-
map = valueMap.computeIfAbsent(equation[1], k -> new HashMap<>());
19-
map.put(equation[0], 1 / values[i]);
14+
add(table, equations[i], values[i]);
2015
}
21-
2216
double[] result = new double[queries.length];
17+
HashSet<String> visited = new HashSet<>();
2318
for (int i = 0; i < queries.length; i++) {
24-
double[] value = new double[] {1.0};
25-
result[i] = dfs(valueMap, queries[i][0], queries[i][1], new HashSet<>(), value) ? value[0] : -1.0;
19+
visited.clear();
20+
result[i] = calc(table, visited, queries[i][0], queries[i][1]);
2621
}
2722
return result;
2823
}
2924

30-
private boolean dfs(HashMap<String, HashMap<String, Double>> map, String start, String end, HashSet<String> set, double[] value) {
31-
if (!map.containsKey(start) || !map.containsKey(end) || set.contains(start)) {
32-
return false;
25+
private double calc(HashMap<String, HashMap<String, Double>> table, HashSet<String> visited, String a, String b) {
26+
HashMap<String, Double> map = table.get(a);
27+
if (map == null) {
28+
return -1.0;
3329
}
34-
if (start.equals(end)) {
35-
return true;
30+
if (a.equals(b)) {
31+
return 1.0;
3632
}
37-
set.add(start);
38-
HashMap<String, Double> valueMap = map.get(start);
39-
boolean flag = false;
40-
for (Map.Entry<String, Double> entry : valueMap.entrySet()) {
41-
value[0] *= entry.getValue();
42-
if (dfs(map, entry.getKey(), end, set, value)) {
43-
flag = true;
44-
break;
33+
for (Map.Entry<String, Double> entry : map.entrySet()) {
34+
String key = entry.getKey();
35+
Double value = entry.getValue();
36+
37+
if (visited.contains(key)) {
38+
continue;
39+
}
40+
41+
visited.add(key);
42+
43+
double t = calc(table, visited, key, b);
44+
if (t != -1.0) {
45+
return value * t;
4546
}
46-
value[0] /= entry.getValue();
47+
48+
visited.remove(key);
49+
}
50+
return -1.0;
51+
}
52+
53+
private void add(HashMap<String, HashMap<String, Double>> table, String[] equation, double value) {
54+
HashMap<String, Double> map0 = table.get(equation[0]);
55+
HashMap<String, Double> map1 = table.get(equation[1]);
56+
if (map0 == null) {
57+
map0 = new HashMap<>();
58+
table.put(equation[0], map0);
59+
}
60+
if (map1 == null) {
61+
map1 = new HashMap<>();
62+
table.put(equation[1], map1);
4763
}
48-
set.remove(start);
49-
return flag;
64+
map0.put(equation[1], value);
65+
map1.put(equation[0], 1 / value);
5066
}
5167
}

leetcode/src/Main.java

Lines changed: 64 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,75 @@
22

33
public class Main {
44

5-
public static int numDecodings(String s) {
6-
int len = s.length();
7-
int[] dp = new int[len];
8-
for (int i = 0; i < len; i++) {
9-
if (s.charAt(i) != '0') {
10-
dp[i] += i > 0 ? dp[i - 1] : 1;
5+
public static class Solution {
6+
public double[] calcEquation(String[][] equations, double[] values, String[][] queries) {
7+
HashMap<String, HashMap<String, Double>> table = new HashMap<>();
8+
for (int i = 0; i < equations.length; i++) {
9+
add(table, equations[i], values[i]);
1110
}
12-
if (i > 0 && (s.charAt(i - 1) == '1' || (s.charAt(i - 1) == '2' && s.charAt(i) <= '6'))) {
13-
dp[i] += i > 1 ? dp[i - 2] : 1;
11+
double[] result = new double[queries.length];
12+
HashSet<String> visited = new HashSet<>();
13+
for (int i = 0; i < queries.length; i++) {
14+
visited.clear();
15+
result[i] = calc(table, visited, queries[i][0], queries[i][1]);
1416
}
17+
return result;
18+
}
19+
20+
private double calc(HashMap<String, HashMap<String, Double>> table, HashSet<String> visited, String a, String b) {
21+
HashMap<String, Double> map = table.get(a);
22+
if (map == null) {
23+
return -1.0;
24+
}
25+
if (a.equals(b)) {
26+
return 1.0;
27+
}
28+
for (Map.Entry<String, Double> entry : map.entrySet()) {
29+
String key = entry.getKey();
30+
Double value = entry.getValue();
31+
32+
if (visited.contains(key)) {
33+
continue;
34+
}
35+
36+
visited.add(key);
37+
38+
double t = calc(table, visited, key, b);
39+
if (t != -1.0) {
40+
return value * t;
41+
}
42+
43+
visited.remove(key);
44+
}
45+
return -1.0;
46+
}
47+
48+
private void add(HashMap<String, HashMap<String, Double>> table, String[] equation, double value) {
49+
HashMap<String, Double> map0 = table.get(equation[0]);
50+
HashMap<String, Double> map1 = table.get(equation[1]);
51+
if (map0 == null) {
52+
map0 = new HashMap<>();
53+
table.put(equation[0], map0);
54+
}
55+
if (map1 == null) {
56+
map1 = new HashMap<>();
57+
table.put(equation[1], map1);
58+
}
59+
map0.put(equation[1], value);
60+
map1.put(equation[0], 1 / value);
1561
}
16-
return dp[len - 1];
1762
}
1863

64+
1965
public static void main(String[] args) {
20-
System.out.println(numDecodings("226"));
66+
Solution s = new Solution();
67+
double[] res = s.calcEquation(new String[][]{
68+
{"a", "b"}, {"b", "c"}
69+
}, new double[]{2.0, 3.0}, new String[][]{
70+
{"a", "c"}, {"b", "a"}, {"a", "e"}, {"a", "a"}, {"x", "x"}
71+
});
72+
for (double v : res) {
73+
System.out.println(v);
74+
}
2175
}
2276
}

0 commit comments

Comments
 (0)