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. 6月辞职->帝都生活

    ---恢复内容开始--- 5月初送走了静,有点伤心,但还是忍住没哭. 纠结了一下上哪个班,上不上基础班,不能再拖了,果断交钱报6月份的ios基础班.之前还有个电话面试,怕怕的,考了很多函数的知识,好多 ...

  2. Off-heap Memory in Apache Flink and the curious JIT compiler

    https://flink.apache.org/news/2015/09/16/off-heap-memory.html   Running data-intensive code in the J ...

  3. java 操作数据库

    package foo;import java.sql.*; public class JdbcDemo { private static Connection conn; private stati ...

  4. SQL实现将一个表的数据插入到另外一个表的代码

    --第一种情况的 1>如果2张表的字段一致,并且希望插入全部数据,可以用这种方法: INSERT INTO 目标表 SELECT * FROM 来源表; 2>比如要将 articles 表 ...

  5. Jquery元素选取、常用方法;js只能获取内联样式,jquery内联内嵌都可以获取到;字符串.trim();去字符串前后空格

    一:常用的选择器: 基本选择器 $("#myDiv") //匹配唯一的具有此id值的元素 $("div") //匹配指定名称的所有元素 $(".myC ...

  6. 利用VS编译libiconv库

    参考文章:http://blog.csdn.net/ghevinn/article/details/9834119 关于中文字符编码问题,这篇文章里面讲的很详细-->http://www.tui ...

  7. Window上装PHP开发环境 (XAMPP)

    原作者:http://www.cnblogs.com/martin1009/archive/2011/11/11/2245794.html 1. 从www.apachefriends.org 上下载X ...

  8. 软件的NABCD----安装部分

    N:需求 有些软件安装需要很多的插件,很很多的安装步骤,甚至文件夹移动等麻烦的步骤 A:做法 做成一键安装的程序 B:好处 默认D盘安装,节省C盘空间,没有赘余插件,一键安装,省时省力. C:竞争 和 ...

  9. css背景图片定位练习(二): background-position的百分比

    background-position:x y; 百分比定位并不能直观的看出来,需要通过计算. background-position百分比计算公式: (容器宽度—背景图片的宽度)*x%=xpx(容器 ...

  10. Sass和Compass制作雪碧图

    1.安装好了sass与compass之后设置一个配置文件 2.新增一个雪碧图文件夹用来存放将要合并的图片例如color文件夹 3.@import命令引用 .Compass看到@import指令的参数为 ...