LeetCode 399. 除法求值

给出方程式 A / B = k, 其中 A 和 B 均为代表字符串的变量, k 是一个浮点型数字。根据已知方程式求解问题,并返回计算结果。如果结果不存在,则返回 -1.0。

示例 : 给定 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 ]

输入为: vector<pair<string, string>> equations, vector& values,
vector<pair<string, string>> queries(方程式,方程式结果,问题方程式), 其中
equations.size() ==
values.size(),即方程式的长度与方程式结果长度相等(程式与结果一一对应),并且结果值均为正数。以上为方程式的描述。
返回vector类型。

基于上述例子,输入如下:

equations(方程式) = [ [“a”, “b”], [“b”, “c”] ], values(方程式结果) = [2.0,
3.0], queries(问题方程式) = [ [“a”, “c”], [“b”, “a”], [“a”, “e”], [“a”, “a”], [“x”, “x”] ]. 输入总是有效的。你可以假设除法运算中不会出现除数为0的情况,且不存在任何矛盾的结果。

  1. 本质上是构造有向图,比如a / b = 2.0, b / c = 3.0,那么a->b->c = 2*3=6。
  2. 由于知道a/b,那么b/a就是倒数,所以图是双向连通但权重不同的边
  3. 可以通过递归做DFS,从起点到终点查到一种结果即可返回,遍历时维护一个visit set,防止出现环状。
public static double[] calcEquation(List<List<String>> equations, double[] values, List<List<String>> queries) {

        Map<String, Double> map = new HashMap<>(equations.size());
        Set<String> set = new HashSet<>();
        for (int i = 0; i < equations.size(); i++) {
            String temp = equations.get(i).get(0) + "/" + equations.get(i).get(1);
            map.put(temp, values[i]);
            temp = equations.get(i).get(1) + "/" + equations.get(i).get(0);
            map.put(temp, 1.0 / values[i]);
            set.add(equations.get(i).get(0));
            set.add(equations.get(i).get(1));
        }
        double[] A = new double[queries.size()];
        for (int i = 0; i < queries.size(); i++) {
            String temp = queries.get(i).get(0) + "/" + queries.get(i).get(1);
            String temp2 = queries.get(i).get(1) + "/" + queries.get(i).get(0);
            if (queries.get(i).get(0).equals(queries.get(i).get(1))) {
                if (set.contains(queries.get(i).get(0))) {
                    A[i] = 1.0;
                } else {
                    A[i] = -1.0;
                }
            } else if (map.containsKey(temp)) {
                A[i] = map.get(temp);
            } else if (map.containsKey(temp2)) {
                A[i] = 1.0 / map.get(temp2);
            } else {
                boolean found = false;
                for (Map.Entry<String, Double> entry : map.entrySet()) {
                    if (entry.getKey().startsWith(queries.get(i).get(0)) && entry.getKey().charAt(queries.get(i).get(0).length()) == '/') {
                        Set<String> visit = new HashSet<>();
                        visit.add(queries.get(i).get(0));
                        if (visit.contains(queries.get(i).get(1))) {
                            continue;
                        }
                        double result = search(
                                entry.getKey().substring(queries.get(i).get(0).length() + 1), queries.get(i).get(1), entry.getValue(), map, visit);
                        if (result != -1.0) {
                            A[i] = result;
                            found = true;
                            break;
                        }
                    }
                }
                if (!found) {
                    A[i] = -1.0;
                }
            }
        }
        return A;
    }

    public static double search(String curStr, String targetStr, double curV, Map<String, Double> map, Set<String> visit) {
        String temp = curStr + "/" + targetStr;
        if (map.containsKey(temp)) {
            return map.get(temp) * curV;
        } else {
            for (Map.Entry<String, Double> entry : map.entrySet()) {
                if (entry.getKey().startsWith(curStr) && entry.getKey().charAt(curStr.length()) == '/') {
                    visit.add(curStr);
                    String nextStr = entry.getKey().substring(curStr.length() + 1);
                    if (visit.contains(nextStr)) {
                        continue;
                    }
                    double result = search(nextStr, targetStr, curV * entry.getValue(), map, visit);
                    if (result != -1.0) {
                        return result;
                    }
                }
            }
            return -1.0;
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值