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

 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);
}
}

Leetcode: Evaluate Division的更多相关文章

  1. [LeetCode] Evaluate Division 求除法表达式的值

    Equations are given in the format A / B = k, where A and B are variables represented as strings, and ...

  2. [Leetcode Week3]Evaluate Division

    Evaluate Division题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/evaluate-division/description/ Desc ...

  3. LN : leetcode 399 Evaluate Division

    lc 399 Evaluate Division 399 Evaluate Division Equations are given in the format A / B = k, where A ...

  4. [LeetCode] 399. Evaluate Division 求除法表达式的值

    Equations are given in the format A / B = k, where A and B are variables represented as strings, and ...

  5. 【LeetCode】399. Evaluate Division 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  6. [leetcode] 399. Evaluate Division

    我是链接 看到这道题,2个点和一个权值,然后想到图,但是leetcode就是这样,没给数据范围,感觉写起来很费劲,然后就开始用图来做,添加边的时候,注意正向边和反向变,然后查询的时候,先判断2个点是否 ...

  7. 【leetcode】399. Evaluate Division

    题目如下: Equations are given in the format A / B = k, whereA and B are variables represented as strings ...

  8. [LeetCode] Evaluate Reverse Polish Notation 计算逆波兰表达式

    Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...

  9. [LeetCode]Evaluate Reverse Polish Notation(逆波兰式的计算)

    原题链接:http://oj.leetcode.com/problems/evaluate-reverse-polish-notation/ 题目描述: Evaluate the value of a ...

随机推荐

  1. 导入maven项目后无法找到sun tools toos-15.0.jar

    直接在缺失该jar包的pom中添加 以下属性和依赖即可! <java.home>D:\devtool\jdk1.6</java.home> <!-- 指定使用的JDK的安 ...

  2. java Channel

    Channel Channel与流 基本上,所有的 IO 在NIO 中都从一个Channel 开始.Channel 有点象流.数据可以从Channel读到Buffer中,也可以从Buffer 写到Ch ...

  3. 鸡肋的Drools

    在看过Drools例子之后,认为其太鸡肋. 完全可以用bshell脚本来代替,或者用java自带的脚本(java5以上才支持,不过不是太好用)代替. 规则文件还要学其新标签,与其配套的接口写那么多,还 ...

  4. CC2540的使用入门

    目录 1. 介绍 2. 开发环境 3. SDCC 1. 介绍 CC2540是一款2.4GHz Bluetooth® low energy SOC,基于8051 MCU 首先,你需要硬件设备 笔者的开发 ...

  5. ubuntu cpus 共享打印

    下载工具 axel 打印机 hp-setup http://blog.x1986.com/t/18.think lsusb wkhtmltopdf/0.12.2.1 ubuntu 14.01 x64下 ...

  6. Linux环境下配置eclipse,以及创建maven工程

    一:maven的安装 1.安装配置maven环境变量 2.验证 二:eclipse的安装 3.解压配置eclipse 4.启动eclipse,必须在虚拟机的eclipse下启动 5.结果 三:修改配置 ...

  7. 将自己写的Python代码打包放到PyPI上

    如果是开源的Python代码,为了能够让大家更方便的使用,放到PyPI上也许是个非常不错的主意(PyPI:Python Package Index).刚开始我以为要将代码打包放到PyPI上是一件非常复 ...

  8. Car---hdu5935(简单题)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5935 题意:有一辆车在马路上行驶,速度不变或增加,然后警察在某整数点时刻记录下了这辆车所经过的位置,共 ...

  9. JS之call/apply/bind

    测试代码: var a = 1; var obj = { a = 2; } function test(a){ alert(a); alert(this.a); } 1.test(3); 结果:3,1 ...

  10. busybox rx 命令

    rx命令使用xmodem传送文件,只需要串口线就传送. 在文件系统输入如下命令,传送文件到板子上,板子上保存文件的名称为file rx file 在secureCRT中选择Transfer->S ...