zoukankan      html  css  js  c++  java
  • Leetcode: 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 public class Solution {
     2     public double[] calcEquation(String[][] equations, double[] values, String[][] queries) {
     3         double[] res = new double[queries.length];
     4         HashMap<String, HashMap<String, Double>> map = new HashMap<String, HashMap<String, Double>>();
     5         for (int i=0; i<equations.length; i++) {
     6             String[] equation = equations[i];
     7             double value = values[i];
     8             if (!map.containsKey(equation[0])) {
     9                 map.put(equation[0], new HashMap<String, Double>());
    10                 map.get(equation[0]).put(equation[0], 1.0);
    11             }
    12             if (!map.containsKey(equation[1])) {
    13                 map.put(equation[1], new HashMap<String, Double>());
    14                 map.get(equation[1]).put(equation[1], 1.0);
    15             }
    16             map.get(equation[0]).put(equation[1], value);
    17             map.get(equation[1]).put(equation[0], 1/value);
    18         }
    19 
    20         for (int j=0; j<queries.length; j++) {
    21             res[j] = -1.0; //initialize
    22             dfs(map, queries[j][0], queries[j][1], new HashSet<String>(), res, j, 1.0);
    23         }
    24         return res;
    25     }
    26     
    27     public void dfs(HashMap<String, HashMap<String, Double>> map, String src, String dest, HashSet<String> visited, double[] res, int index, double pathVal) {
    28         if (!map.containsKey(src) || !map.containsKey(dest)) {
    29             res[index] = -1.0;
    30             return;
    31         }
    32         if (visited.contains(src)) return;
    33         HashMap<String, Double> srcNb = map.get(src);
    34         if (srcNb.containsKey(dest)) {
    35             res[index] = pathVal * srcNb.get(dest);
    36             return;
    37         }
    38         visited.add(src);
    39         for (Map.Entry<String, Double> entry : srcNb.entrySet()) {
    40             String neibor = entry.getKey();
    41             dfs(map, neibor, dest, visited, res, index, pathVal*entry.getValue());
    42         }
    43         visited.remove(src);
    44     }
    45 }
  • 相关阅读:
    Centos7.2升级内核至3.10.0-957【转】
    部署一套完整的Kubernetes高可用集群(上)【转】
    Nginx配置中一个不起眼字符"/"的巨大作用,失之毫厘谬以千里【转】
    使用vmware搭建k8s集群【转】
    nginx的request body日志格式配置
    nginx漏洞修复:SSL/TLS 服务器瞬时 Diffie-Hellman 公共密钥过弱【原理扫描】【转】
    nginx:[warn] the "ssl" directive is deprecated, use the "listen ... ssl" directive instead
    linux系统删除分区
    Linux用户锁定、解锁及锁定查看
    innobackupex远程备份【转】
  • 原文地址:https://www.cnblogs.com/EdwardLiu/p/6124304.html
Copyright © 2011-2022 走看看