mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-01-11 02:58:13 +08:00
55 lines
3.3 KiB
HTML
55 lines
3.3 KiB
HTML
<p>给定一个变量对数组 <code>equations</code> 和一个实数值数组 <code>values</code> 作为已知条件,其中 <code>equations[i] = [A<sub>i</sub>, B<sub>i</sub>]</code> 和 <code>values[i]</code> 共同表示等式 <code>A<sub>i</sub> / B<sub>i</sub> = values[i]</code> 。每个 <code>A<sub>i</sub></code> 或 <code>B<sub>i</sub></code> 是一个表示单个变量的字符串。</p>
|
||
|
||
<p>另有一些以数组 <code>queries</code> 表示的问题,其中 <code>queries[j] = [C<sub>j</sub>, D<sub>j</sub>]</code> 表示第 <code>j</code> 个问题,请你根据已知条件找出 <code>C<sub>j</sub> / D<sub>j</sub> = ?</code> 的结果作为答案。</p>
|
||
|
||
<p>返回 <strong>所有问题的答案</strong> 。如果存在某个无法确定的答案,则用 <code>-1.0</code> 替代这个答案。如果问题中出现了给定的已知条件中没有出现的字符串,也需要用 <code>-1.0</code> 替代这个答案。</p>
|
||
|
||
<p><strong>注意:</strong>输入总是有效的。可以假设除法运算中不会出现除数为 0 的情况,且不存在任何矛盾的结果。</p>
|
||
|
||
<p> </p>
|
||
|
||
<p><strong>示例 1:</strong></p>
|
||
|
||
<pre>
|
||
<strong>输入:</strong>equations = [["a","b"],["b","c"]], values = [2.0,3.0], queries = [["a","c"],["b","a"],["a","e"],["a","a"],["x","x"]]
|
||
<strong>输出:</strong>[6.00000,0.50000,-1.00000,1.00000,-1.00000]
|
||
<strong>解释:</strong>
|
||
条件:<em>a / b = 2.0</em>, <em>b / c = 3.0</em>
|
||
问题:<em>a / c = ?</em>, <em>b / a = ?</em>, <em>a / e = ?</em>, <em>a / a = ?</em>, <em>x / x = ?</em>
|
||
结果:[6.0, 0.5, -1.0, 1.0, -1.0 ]
|
||
</pre>
|
||
|
||
<p><strong>示例 2:</strong></p>
|
||
|
||
<pre>
|
||
<strong>输入:</strong>equations = [["a","b"],["b","c"],["bc","cd"]], values = [1.5,2.5,5.0], queries = [["a","c"],["c","b"],["bc","cd"],["cd","bc"]]
|
||
<strong>输出:</strong>[3.75000,0.40000,5.00000,0.20000]
|
||
</pre>
|
||
|
||
<p><strong>示例 3:</strong></p>
|
||
|
||
<pre>
|
||
<strong>输入:</strong>equations = [["a","b"]], values = [0.5], queries = [["a","b"],["b","a"],["a","c"],["x","y"]]
|
||
<strong>输出:</strong>[0.50000,2.00000,-1.00000,-1.00000]
|
||
</pre>
|
||
|
||
<p> </p>
|
||
|
||
<p><strong>提示:</strong></p>
|
||
|
||
<ul>
|
||
<li><code>1 <= equations.length <= 20</code></li>
|
||
<li><code>equations[i].length == 2</code></li>
|
||
<li><code>1 <= A<sub>i</sub>.length, B<sub>i</sub>.length <= 5</code></li>
|
||
<li><code>values.length == equations.length</code></li>
|
||
<li><code>0.0 < values[i] <= 20.0</code></li>
|
||
<li><code>1 <= queries.length <= 20</code></li>
|
||
<li><code>queries[i].length == 2</code></li>
|
||
<li><code>1 <= C<sub>j</sub>.length, D<sub>j</sub>.length <= 5</code></li>
|
||
<li><code>A<sub>i</sub>, B<sub>i</sub>, C<sub>j</sub>, D<sub>j</sub></code> 由小写英文字母与数字组成</li>
|
||
</ul>
|
||
|
||
<p> </p>
|
||
|
||
<p><meta charset="UTF-8" />注意:本题与主站 399 题相同: <a href="https://leetcode-cn.com/problems/evaluate-division/">https://leetcode-cn.com/problems/evaluate-division/</a></p>
|