【LeetCode 面试经典150题】399. Evaluate Division 计算除法

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]。每个 AiBi 是表示单个变量的字符串。

你还会得到一些查询,其中 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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值