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
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的更多相关文章
- [LeetCode] Evaluate Division 求除法表达式的值
Equations are given in the format A / B = k, where A and B are variables represented as strings, and ...
- [Leetcode Week3]Evaluate Division
Evaluate Division题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/evaluate-division/description/ Desc ...
- LN : leetcode 399 Evaluate Division
lc 399 Evaluate Division 399 Evaluate Division Equations are given in the format A / B = k, where A ...
- [LeetCode] 399. Evaluate Division 求除法表达式的值
Equations are given in the format A / B = k, where A and B are variables represented as strings, and ...
- 【LeetCode】399. Evaluate Division 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- [leetcode] 399. Evaluate Division
我是链接 看到这道题,2个点和一个权值,然后想到图,但是leetcode就是这样,没给数据范围,感觉写起来很费劲,然后就开始用图来做,添加边的时候,注意正向边和反向变,然后查询的时候,先判断2个点是否 ...
- 【leetcode】399. Evaluate Division
题目如下: Equations are given in the format A / B = k, whereA and B are variables represented as strings ...
- [LeetCode] Evaluate Reverse Polish Notation 计算逆波兰表达式
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
- [LeetCode]Evaluate Reverse Polish Notation(逆波兰式的计算)
原题链接:http://oj.leetcode.com/problems/evaluate-reverse-polish-notation/ 题目描述: Evaluate the value of a ...
随机推荐
- Connection Management and Security
High Performance My SQL THIRD EDITION Each client connection gets its own thread within the server ...
- Progressive enhancement
https://en.wikipedia.org/wiki/Progressive_enhancement Progressive enhancement is a strategy for web ...
- cursor:pointer
.cursor_hand{ cursor:pointer; }
- 蓝牙—GAP(Generic Access Profile)
1.简介 下图可见GAP在蓝牙协议中的位置和关系 LE中GAP共有四个角色: <1> Boradcaster:发送advertising 事件的设备 <2>Observer:接 ...
- mysql-zabbix-agent
使用Zabbix监控MySQL服务器方法 01/27/2014 从Zabbix 2.2开始,Zabbix官方已经支持了MySQL监控,但是MySQL监控默认是不可用的,需要经过额外的设置才可以使用.K ...
- android source compiler
- Android 环境快速搭建-详细步骤-win7-64bit
电脑装了win7 64位的系统,重新来搭建了安卓环境,发现有一种非常便捷,快速的方法就可以搭建起来了~ 步骤一:下载java sdk 进入http://www.oracle.com/us/sun/in ...
- .NET对象与Windows句柄(二):句柄分类和.NET句柄泄露的例子
上一篇文章介绍了句柄的基本概念,也描述了C#中创建文件句柄的过程.我们已经知道句柄代表Windows内部对象,文件对象就是其中一种,但显然系统中还有更多其它类型的对象.本文将简单介绍Windows对象 ...
- javascript 模式方面的学习
看了好多网上的文章,基本上得到一个结论:一些写类工具函数或框架的写类方式本质上都是 构造函数+原型 1.用构造函数来定义类属性(字段).2.用原型方式来定义类的方法. 具体文章请参阅 JavaScri ...
- Magento SSH 下载安装
http://www.magentocommerce.com/wiki/1_-_installation_and_configuration/installing_magento_via_shell_ ...