Leetcode 399. Evaluate Division 除法推理 解题报告

1 解题思想

这道题是说,给了很多a/b,c/d,a/c 等等的除法的等式,现在再给一些新的等式,让推断能否从已有的等式当中找到结果,能的话就输出,不能的话就-1;

这道题首先需要做的工作是,已知a/b=x的情况下,推断出b/a=1/x的情况。将所有a作为被除数的情况下能作为除数的全部集合起来。

然后就有搜索的方式(如DFS),搜索有没有一个这样的链:

a/c = a/x1 x1/x2 x2/x3*…..*xn/c
这样就能推导出最终的结果了

总之原理很简单,实现比较麻烦

突然写了4题leetcode,又写了对应的解题报告,略显累,写不太好了,有问题评论解答

2 原题

Equations are given in the format A / B = k, where A and B are variables represented as strings, and k is a real number (floating point number). Given some queries, return the answers. If the answer does not exist, return -1.0.

Example:
Given a / b = 2.0, b / c = 3.0. 
queries are: a / c = ?, b / a = ?, a / e = ?, a / a = ?, x / x = ? . 
return [6.0, 0.5, -1.0, 1.0, -1.0 ].

The input is: vector<pair<string, string>> equations, vector<double>& values, vector<pair<string, string>> queries , where equations.size() == values.size(), and the values are positive. This represents the equations. Return vector<double>.

According to the example above:

equations = [ ["a", "b"], ["b", "c"] ],
values = [2.0, 3.0],
queries = [ ["a", "c"], ["b", "a"], ["a", "e"], ["a", "a"], ["x", "x"] ]. 
The input is always valid. You may assume that evaluating the queries will result in no division by zero and there is no contradiction.

3 AC解

public class Solution {
      //存储关系,及被除数开头的情况下,有哪些除数
    HashMap<String,List<String>> divisionRelations = new HashMap<String,List<String>>();
    HashMap<String,Double> equationsResult = new HashMap<String,Double>();
    /**
     * 搜索那些那可以
     * 要么 a/b 存在
     * 要么有 (a/c)*(c/b)存在,二选一
     * */
    public double dfs(HashSet<String> visited,double val,String a,String b){
        double result = -1;
        visited.add(a);
        if(divisionRelations.containsKey(a) == false)
        {
            visited.remove(a);
            return result;
        }
        List<String> list = divisionRelations.get(a);
        for(String tmp:list){
            if(tmp.equals(b)){
                result = equationsResult.get(a+"%"+tmp);
                break;
            } else if (visited.contains(tmp) == false){
                double midResult = dfs(visited,val,tmp,b);
                if(midResult!=-1){
                    result = equationsResult.get(a+"%"+tmp)*midResult;
                    break;
                }

            }
        }
        visited.remove(a);
        return result;
    }
    public double[] calcEquation(String[][] equations, double[] values, String[][] queries) {

      /**
       * 保存好关系 a/b 以及对应的 b/a,注意a/b = 0的时候,没有其他*/
        for(int i=0;i<equations.length;i++){
            String a = equations[i][0];
            String b = equations[i][1];
            if(divisionRelations.containsKey(a) == false) divisionRelations.put(a,new ArrayList<String>());
             if(divisionRelations.containsKey(b) == false) divisionRelations.put(b,new ArrayList<String>());
            divisionRelations.get(a).add(b);
            equationsResult.put(a+"%"+b,values[i]);
            if(values[i]!=0){
                 divisionRelations.get(b).add(a);
            equationsResult.put(b+"%"+a,1.0/values[i]);
            }
        }
        //运算
        double[] result = new double[queries.length];
        for(int i=0;i<queries.length;i++){
            result[i] = dfs(new HashSet<String>(),0,queries[i][0],queries[i][1]);
        }
        return result;


    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值