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<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.

Graph, DFS

(1) Build the map, the key is dividend, the value is also a map whose key is divisor and value is its parameter. For example, a / b = 2.0, the map entry is <"a", <"b", 2.0>>. To make searching and calculation easier, we also put b / a = 0.5 into the map.
(2) for each query, use DFS to search divisors recursively

1.hashmap 建图

2. dfs 递归搜索

public class Solution {
    public double[] calcEquation(String[][] equations, double[] values, String[][] queries) {
        double[] res = new double[queries.length];
        HashMap<String, HashMap<String, Double>> map = new HashMap<String, HashMap<String, Double>>();
        for (int i=0; i<equations.length; i++) {
            String[] equation = equations[i];
            double value = values[i];
            if (!map.containsKey(equation[0])) {
                map.put(equation[0], new HashMap<String, Double>());
                map.get(equation[0]).put(equation[0], 1.0);
            }
            if (!map.containsKey(equation[1])) {
                map.put(equation[1], new HashMap<String, Double>());
                map.get(equation[1]).put(equation[1], 1.0);
            }
            map.get(equation[0]).put(equation[1], value);
            map.get(equation[1]).put(equation[0], 1/value);
        }

        for (int j=0; j<queries.length; j++) {
            res[j] = -1.0; //initialize
            dfs(map, queries[j][0], queries[j][1], new HashSet<String>(), res, j, 1.0);
        }
        return res;
    }
    
    public void dfs(HashMap<String, HashMap<String, Double>> map, String src, String dest, 
            HashSet<String> visited, double[] res, int index, double pathVal) { if (!map.containsKey(src) || !map.containsKey(dest)) { res[index] = -1.0; return; } if (visited.contains(src)) return; HashMap<String, Double> srcNb = map.get(src); if (srcNb.containsKey(dest)) { res[index] = pathVal * srcNb.get(dest); return; } visited.add(src); for (Map.Entry<String, Double> entry : srcNb.entrySet()) { String neibor = entry.getKey(); dfs(map, neibor, dest, visited, res, index, pathVal*entry.getValue()); } visited.remove(src); } }

 

dfs根据返回值是否在输入值里面确定是否是void, 像记忆化搜索就不是

  0, 递归输入值: 存结果的容器, 题意的目标查找值, 标记走过路的容器, 或者开始走的指针, 要dfs的容器, 走过的路带来的结果值

  1 关键是递归出口分为: 1 找不到直接返回, 2走过的路也返回(递归完)  

  2,找到搜素目标, 存入结果容器, 返回

  3 标记走过的路(hashset 存储法,搜索容器去除法, 存为string存储法, 开始指针移动法)

  4 设置循环继续遍历相应的路, 得看dfs的容器是什么样的才知道如何设置循环

  5, 看一看下一层dfs的函数的输入值如何改变

  6, 循环遍历完, 记得去除标记走过 的路, 为了上一层不同的dfs递归到下一层时可以走这条路.

  

 

posted @ 2017-07-13 18:49  apanda009  阅读(118)  评论(0编辑  收藏  举报