|
6 | 6 | import java.util.List;
|
7 | 7 | import java.util.Map;
|
8 | 8 |
|
9 |
| -/** |
10 |
| - * 399. Evaluate Division |
11 |
| - * |
12 |
| - * Equations are given in the format A / B = k, where A and B are variables represented as strings, and k is a real number (floating point number). |
13 |
| - * Given some queries, return the answers. If the answer does not exist, return -1.0. |
14 |
| -
|
15 |
| - Example: |
16 |
| - Given a / b = 2.0, b / c = 3.0. |
17 |
| - queries are: a / c = ?, b / a = ?, a / e = ?, a / a = ?, x / x = ? . |
18 |
| - return [6.0, 0.5, -1.0, 1.0, -1.0 ]. |
19 |
| -
|
20 |
| - The input is: vector<pair<string, string>> equations, vector<double>& values, vector<pair<string, string>> queries , |
21 |
| - where equations.size() == values.size(), and the values are positive. This represents the equations. Return vector<double>. |
22 |
| -
|
23 |
| - According to the example above: |
24 |
| -
|
25 |
| - equations = [ ["a", "b"], ["b", "c"] ], |
26 |
| - values = [2.0, 3.0], |
27 |
| - queries = [ ["a", "c"], ["b", "a"], ["a", "e"], ["a", "a"], ["x", "x"] ]. |
28 |
| -
|
29 |
| - The input is always valid. You may assume that evaluating the queries will result in no division by zero and there is no contradiction. |
30 |
| - */ |
31 | 9 | public class _399 {
|
32 | 10 |
|
33 | 11 | public static class Solution1 {
|
34 | 12 | /**
|
35 | 13 | * Credit: https://discuss.leetcode.com/topic/59146/java-ac-solution-using-graph
|
36 |
| - * |
| 14 | + * <p> |
37 | 15 | * Image a/b = k as a link between node a and b, the weight from a to b is k, the reverse link
|
38 | 16 | * is 1/k. Query is to find a path between two nodes.
|
39 | 17 | */
|
@@ -68,7 +46,7 @@ public double[] calcEquation(String[][] equations, double[] values, String[][] q
|
68 | 46 | }
|
69 | 47 |
|
70 | 48 | private double dfs(String start, String end, Map<String, List<String>> pairs,
|
71 |
| - Map<String, List<Double>> valuePairs, HashSet<String> set, double value) { |
| 49 | + Map<String, List<Double>> valuePairs, HashSet<String> set, double value) { |
72 | 50 | if (set.contains(start)) {
|
73 | 51 | return 0.0;
|
74 | 52 | }
|
|
0 commit comments