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