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: vectorstring, string>> equations, vector<double>& values, vectorstring, 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> divisionRelations = new HashMap>();
    HashMap equationsResult = new HashMap();
    /**
     * 搜索那些那可以
     * 要么 a/b 存在
     * 要么有 (a/c)*(c/b)存在,二选一
     * */
    public double dfs(HashSet visited,double val,String a,String b){
        double result = -1;
        visited.add(a);
        if(divisionRelations.containsKey(a) == false)
        {
            visited.remove(a);
            return result;
        }
        List 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;i0];
            String b = equations[i][1];
            if(divisionRelations.containsKey(a) == false) divisionRelations.put(a,new ArrayList());
             if(divisionRelations.containsKey(b) == false) divisionRelations.put(b,new ArrayList());
            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;inew HashSet(),0,queries[i][0],queries[i][1]);
        }
        return result;


    }
}

你可能感兴趣的:(leetcode-java,leetcode,除法,division,evaluate,推导)