# leetcode399. Evaluate Division

## 题目要求

```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). Given some queries, return the answers. If the answer does not exist, return -1.0.

Example:
Given a / b = 2.0, b / c = 3.0.
queries are: a / c = ?, b / a = ?, a / e = ?, a / a = ?, x / x = ? .
return [6.0, 0.5, -1.0, 1.0, -1.0 ].

The input is: vector<pair<string, string>> equations, vector<double>& values, vector<pair<string, string>> queries , where equations.size() == values.size(), and the values are positive. This represents the equations. Return vector<double>.

According to the example above:

equations = [ ["a", "b"], ["b", "c"] ],
values = [2.0, 3.0],
queries = [ ["a", "c"], ["b", "a"], ["a", "e"], ["a", "a"], ["x", "x"] ].

The input is always valid. You may assume that evaluating the queries will result in no division by zero and there is no contradiction.```

## 思路和代码

```    public double[] calcEquation(List<List<String>> equations, double[] values, List<List<String>> queries) {
//图的链式表示法
Map<String, List<String>> pairs = new HashMap<>();
//图上每条边的权重
Map<String, List<Double>> valuedPairs = new HashMap<>();
for(int i = 0 ; i < equations.size() ; i++) {
//获取第i个方程式
List<String> equation = equations.get(i);
String multiplied = equation.get(0);//被除数
String multiplier = equation.get(1);//除数
//如果被除数从来没有添加到图中，则将其作为顶点在图中初始化
if(!pairs.containsKey(multiplied)) {
pairs.put(multiplied, new ArrayList<>());
valuedPairs.put(multiplied, new ArrayList<>());
}
//如果除数从来没有添加到图中，则将其作为顶点在图中初始化
if(!pairs.containsKey(multiplier)) {
pairs.put(multiplier, new ArrayList<>());
valuedPairs.put(multiplier, new ArrayList<>());
}
//添加边和边的权重
}

//结果集
double[] result = new double[queries.size()];
for(int i = 0 ; i<queries.size() ; i++) {
//在图中，以被除数作为顶点，深度优先遍历图，直到找到值为除数的顶点
result[i] = dfs(queries.get(i).get(0), queries.get(i).get(1), pairs, valuedPairs, new HashSet<>(), 1.0);
result[i] = result[i]==0.0 ? -1.0 : result[i];
}
return result;
}

public double dfs(String multiplied, String multiplier, Map<String, List<String>> pairs, Map<String, List<Double>> valuedPairs, Set<String> visited, double curResult) {
//如果图中不包含该被除数顶点，则无法获知该表达式的值
if(!pairs.containsKey(multiplied)) return 0.0;
//如果再次访问过该被除数，说明找到了一条环路，则该深度优先遍历结果失败，直接抛弃
if(visited.contains(multiplied)) return 0.0;
//如果被除数等于除数，则返回1.0
if(multiplied.equals(multiplier)) return curResult;
//获得当前被除数的所有邻接顶点
List<String> multipliers = pairs.get(multiplied);
//获得所有邻接边的权重
List<Double> multiplierValues = valuedPairs.get(multiplied);
double tmp = 0.0;
for(int i = 0 ; i<multipliers.size() ; i++) {
//以邻接点为新的顶点，继续深度优先遍历
//此时调用方法中curResult的值代表的是该原邻接点除以邻接点的值
//如 a/b=2, b/c=3, 则a=2b，因此当我们以b作为邻接点寻找c时，需要记录原被除数是现被除数的两倍
tmp = dfs(multipliers.get(i), multiplier, pairs, valuedPairs, visited, curResult * multiplierValues.get(i));
//找到非零路径，结束深度优先遍历
if(tmp != 0.0){
break;
}
}
visited.remove(multiplied);
return tmp;
}```

0 条评论