399. Evaluate Division(计算除法)
题目大意
You are given an array of variable pairs equations
and an array of real numbers values
, where equations[i] = [Ai, Bi]
and values[i]
represent the equation Ai / Bi = values[i]
. Each Ai
or Bi
is a string that represents a single variable.
You are also given some queries, where queries[j] = [Cj, Dj]
represents the jth
query where you must find the answer for Cj / Dj = ?
.
Return the answers to all queries. If a single answer cannot be determined, return -1.0.
Note: The input is always valid. You may assume that evaluating the queries will not result in division by zero and that there is no contradiction.
中文释义
给你一个变量对数组 equations
和一个实数数组 values
,其中 equations[i] = [Ai, Bi]
和 values[i]
表示方程 Ai / Bi = values[i]
。每个 Ai
或 Bi
是表示单个变量的字符串。
你还会得到一些查询,其中 queries[j] = [Cj, Dj]
表示第 j
个查询,你需要找出 Cj / Dj = ?
的答案。
返回所有查询的答案。如果无法确定单个答案,返回 -1.0。
注意:输入总是有效的。你可以假设评估查询不会导致除以零的情况,也不会有矛盾。
示例
- 示例 1:
- 输入:
equations = [["a","b"],["b","c"]]
,values = [2.0,3.0]
,queries = [["a","c"],["b","a"],["a","e"],["a","a"],["x","x"]]
- 输出:
[6.00000,0.50000,-1.00000,1.00000,-1.00000]
- 解释:
已知:a / b = 2.0, b / c = 3.0
查询:a / c = ?, b / a = ?, a / e = ?, a / a = ?, x / x = ?
返回:[6.0, 0.5, -1.0, 1.0, -1.0 ]
注意:x 未定义 => -1.0
- 输入:
- 示例 2:
- 输入:
equations = [["a","b"],["b","c"],["bc","cd"]]
,values = [1.5,2.5,5.0]
,queries = [["a","c"],["c","b"],["bc","cd"],["cd","bc"]]
- 输出:
[3.75000,0.40000,5.00000,0.20000]
- 输入:
- 示例 3:
- 输入:
equations = [["a","b"]]
,values = [0.5]
,queries = [["a","b"],["b","a"],["a","c"],["x","y"]]
- 输出:
[0.50000,2.00000,-1.00000,-1.00000]
- 输入:
限制条件
1 <= equations.length <= 20
equations[i].length == 2
1 <= Ai.length, Bi.length <= 5
values.length == equations.length
0.0 < values[i] <= 20.0
1 <= queries.length <= 20
queries[i].length == 2
1 <= Cj.length, Dj.length <= 5
Ai, Bi, Cj, Dj
由小写英文字母和数字组成。
解题思路
使用深度优先搜索(DFS)来解决问题。构建一个图来表示变量之间的除法关系,并使用 DFS 在图中搜索给定查询的答案。
步骤说明
- 构建一个图,其中每个节点是一个变量,每条边代表两个变量之间的除法关系。
- 遍历每个查询:
- 如果查询的变量不存在于图中,返回 -1.0。
- 否则,使用 DFS 在图中搜索从
Cj
到Dj
的路径,并计算除法结果。
- 返回所有查询的结果。
代码
class Solution {
public:
unordered_map<string, unordered_map<string, double>> buildGraph(vector<vector<string>>& equations, vector<double>& values) {
unordered_map<string, unordered_map<string, double>> graph;
for (int i = 0; i < equations.size(); i++) {
graph[equations[i][0]][equations[i][1]] = values[i];
graph[equations[i][1]][equations[i][0]] = 1.0 / values[i];
}
return graph;
}
double findTarget(string& src, string& dst, unordered_map<string, unordered_map<string, double>>& graph, unordered_set<string>& visited) {
if (graph.find(src) == graph.end()) return -1.0;
if (graph[src].find(dst) != graph[src].end()) return graph[src][dst];
visited.insert(src);
for (auto& neighbor: graph[src]) {
if (visited.find(neighbor.first) == visited.end()) {
string new_src = neighbor.first;
double product = findTarget(new_src, dst, graph, visited);
if (product != -1.0) {
return neighbor.second * product * 1.0;
}
}
}
return -1.0;
}
vector<double> calcEquation(vector<vector<string>>& equations, vector<double>& values, vector<vector<string>>& queries) {
unordered_map<string, unordered_map<string, double>> graph = buildGraph(equations, values);
vector<double> results;
for (auto& query: queries) {
unordered_set<string> visited;
double ans = findTarget(query[0], query[1], graph, visited);
results.push_back(ans);
}
return results;
}
};