399. Evaluate Division

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> equations, vector& values, vector> queries , where equations.size() == values.size(), and the values are positive. This represents the equations. Return vector.

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.

一刷
题解:
一种最简单的做法:
构建一个map>, key为分子,value为<分母, value>. 然后有个对称的,比如>, >。并且每次加入一个equation, 就一次迭代分子,分母的map. 最后只用在map里面查表即可。

public class Solution {
    public double[] calcEquation(String[][] equations, double[] values, String[][] queries) {
        HashMap> map = new HashMap<>();
        for(int i=0; i());
            if(!map.containsKey(eq[1]))
                map.put(eq[1], new HashMap<>());
            
            HashMap map0 = map.get(eq[0]);
            HashMap map1 = map.get(eq[1]);
            map0.put(eq[1], values[i]);
            map1.put(eq[0], 1/values[i]);
            
            insert(map, eq[0], eq[1], values[i]);
        }
        int len = queries.length;
        double[] re = new double[len];
        for(int i=0; i> map, String a, String b, double value) {
        HashMap map0 = map.get(a);
        HashMap map1 = map.get(b);
        for( String key : map1.keySet()) {
            double val = map1.get(key)*value;
            if(!map0.containsKey(key)){
                map0.put(key, val);
                map.get(key).put(a, 1/val);
                insert(map, a, key, val);
            }
        }
        for( String key : map0.keySet()) {
            double val = map0.get(key)/value;
            if(!map1.containsKey(key)){
                map1.put(key, val);
                map.get(key).put(b, 1/val);
                insert(map, b, key, val);
            }
        }
    }
}

Speed up
相当于给了root一个基准值,如果a/b=2, 那么b->a (在root中),在b=2, a=1(value map中)

于是得到了每个character对应的root

class Solution {
    public double[] calcEquation(String[][] equations, double[] values, String[][] queries) {
        if(equations==null || equations.length==0) return new double [] {};
        
        Maproot=new HashMap<>();
        Mapmap=new HashMap<>();
        
        for (int i=0;iroot, String var){
        if (root.get(var).equals(var)) return var;
        return find(root, root.get(var));
    }
    
    private double get(Maproot, Mapmap, String var){
        String r=root.get(var);
        double result=map.get(var);
        
        if (r.equals(var)) return result;
        return result*get(root, map, r);
    }

}

要用图算法来解,因为(A/B)(B/C)(C/D) is like the path A->B->C->D。例如Floyd-Warshall算法是解决两点间的最短路径的一种算法。

399. Evaluate Division_第1张图片
Floyd-Algorithm

你可能感兴趣的:(399. Evaluate Division)