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 ...
随机推荐
- Swapping
COMPUTER ORGANIZATION AND ARCHITECTURE DESIGNING FOR PERFORMANCE NINTH EDITION Referring back to Fig ...
- php://input,$_POST,$HTTP_RAW_POST_DATA区别
我们先来看两个demo 例子:php://input 代码如下 post.php 代码如下 例子:$_post 代码如下 welcome.php 代码如下 再来看$GLOBALS [& ...
- 【转】基于laravel制作APP接口(API)
这篇文章主要介绍了基于laravel制作APP接口(API)的相关资料,需要的朋友可以参考下 前期准备 前言,为什么做以及要做个啥本人姓小名白,不折不扣编程届小白一名,但是自从大一那会儿接触到编程这件 ...
- jbpm node signal
task-node (任务节点) 其性质和node节点一样,在没有task的时候,也都是自动执行,不等待.task-node被归类为一个等待节点,是指在task-node中的task列表中的task没 ...
- IIS绑定Active Directory账号自动登录网站的方法
满足使用Request.ServerVariables["REMOTE_USER"]的条件: 1.IIS配置网站的目录安全性取消“启用匿名访问(&A)” 2.启用 “集成 ...
- Mschat控件示例升级错误处理方法
将具有 3.5 版图表控件的 ASP.NET 3.5 网站升级到 ASP.NET 4 需要更改 web.config 和注册指令 将具有 3.5 版图表控件的 ASP.NET 3.5 网站升级到 AS ...
- library not found for -lPods 的解决办法
在老项目工程中使用cocoapods,可能会报这个错误:library not found for -lPods . 导致这个错误可能有两个原因,这两个原因在编译过程中都是有蛛丝马迹可循的. 原因1: ...
- Mixing Delphi and C++(相互调用)
Mixing Delphi and C++ You have a TStringList and <algorithm>. What can you do? Quite a lot, ac ...
- java中的trim()
trim():去掉字符串首尾的空格.但该方法并不仅仅是去除空格,它能够去除从编码'\u0000′ 至 '\u0020′ 的所有字符. 回车换行也在这20个字符 例1: public static vo ...
- 从while(cin>>a)开始探讨cin
1. 首先cin>>a返回的是左操作数,也就是返回cin. cin的条件状态中: cin.eof() 判断流是否到达文件的结束符 cin.fail() 判断IO操作是否失败 在 ...