399. Evaluate Division
图像题,没觉得有什么简单的办法,貌似可以用Union Find来解。
感觉有2种思路,一种是先全部构建好每2个点的weight,然后直接遍历queires[][]来抓取答案。
一种是只构建简单的关系图,然后通过DFS来一一找寻要求的答案。
做了一会好他妈的麻烦,最后参考了(http://blog.csdn.net/yeqiuzs/article/details/52506433)
用的是第二种。
需要注意,不存在 优先于 相等 eg : x/x = -1.0, not 1.0, if x DNE in map.
public class Solution
{
public double[] calcEquation(String[][] equations, double[] values, String[][] queries)
{
double[] res = new double[queries.length];
Map<String,Map<String,Double>> map = new HashMap<String,Map<String,Double>>();
Set<String> set = new HashSet<String>();
for(int i = 0; i < equations.length;i++)
{
set.add(equations[i][0]);
set.add(equations[i][1]);
Map<String,Double> tempMap;
if(!map.containsKey(equations[i][0]))
{
tempMap = new HashMap<String,Double>();
}
else
{
tempMap = map.get(equations[i][0]);
}
tempMap.put(equations[i][1],values[i]);
map.put(equations[i][0], tempMap);
if(!map.containsKey(equations[i][1]))
{
tempMap = new HashMap<String,Double>();
}
else
{
tempMap = map.get(equations[i][1]);
}
tempMap.put(equations[i][0],1.0/values[i]);
map.put(equations[i][1],tempMap);
}
//cal
for(int i = 0; i < queries.length;i++)
{
String a = queries[i][0];
String b = queries[i][1];
if(a.equals(b) && map.containsKey(a))
{
res[i] = 1.0;
continue;
}
Map<String,Boolean> available = new HashMap<String,Boolean>();
Iterator iter = set.iterator();
while(iter.hasNext())
{
available.put((String)iter.next(),true);
}
double tempRes = helper(map,available,a,b,1.0);
res[i] = tempRes;
}
return res;
}
public double helper(Map<String,Map<String,Double>> map, Map<String,Boolean> available,String a, String b, double base)
{
//new entry
if(map.containsKey(a) && available.get(a))
{
available.put(a,false);
Map<String,Double> tempMap = map.get(a);
if(tempMap.containsKey(b)) return base * tempMap.get(b);
else
{
Iterator iter = tempMap.keySet().iterator();
double tempRes = -1.0;
while(iter.hasNext() && tempRes == -1.0)
{
String tempB = (String)iter.next();
//available.put(tempB,false);
tempRes = helper(map,new HashMap<String,Boolean>(available),tempB,b,base * tempMap.get(tempB));
//available.put(tempB,true);
}
if(tempRes == -1.0) return -1.0;
else return tempRes;
}
}
else
{
return -1.0;
}
}
}
考基本功的。二刷看看能不能先构建完整的图。
399. Evaluate Division的更多相关文章
- 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
题目如下: Equations are given in the format A / B = k, whereA and B are variables represented as strings ...
- 【LeetCode】399. Evaluate Division 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- [leetcode] 399. Evaluate Division
我是链接 看到这道题,2个点和一个权值,然后想到图,但是leetcode就是这样,没给数据范围,感觉写起来很费劲,然后就开始用图来做,添加边的时候,注意正向边和反向变,然后查询的时候,先判断2个点是否 ...
- [Leetcode Week3]Evaluate Division
Evaluate Division题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/evaluate-division/description/ Desc ...
- [LeetCode] Evaluate Division 求除法表达式的值
Equations are given in the format A / B = k, where A and B are variables represented as strings, and ...
- Leetcode: Evaluate Division
Equations are given in the format A / B = k, where A and B are variables represented as strings, and ...
- [Swift]LeetCode399. 除法求值 | Evaluate Division
Equations are given in the format A / B = k, where A and B are variables represented as strings, and ...
随机推荐
- windows下的SASS/Compass的安装与卸载
认识SASS/Compass SASS是一种CSS的开发工具,提供了许多便利的写法,大大节省了设计者的时间,使得CSS的开发,变得简单和可维护. SASS与Compass的安装说明 SASS在Wind ...
- word中几个好用的宏代码(立方米上标、关闭样式自动更新、删除无效样式、表格加粗边框、宋体引号)
Sub 替换立方米() With Selection.Find .Text = "m3" .Replacement.Text = "mm3" .Forward ...
- Vcl.FileCtrl.SelectDirectory
描述:显示一个目录的对话框(属于Delphi) function SelectDirectory(var Directory: string; Options: TSelectDirOpts; Hel ...
- Csharp Winfrom 多串口通信
Csharp 多串口通信 顾名思义,多串口通信,普通的PC机一般只有一个串口,现在很多家用的PC都没有串口,那么问题来了,如何保证多串口呢? 有一种神器,MOXA CP-168U Series PCI ...
- linq any()方法实现sql in()方法的效果
public IQueryable<Vsec009ComSecComp> QueryList(Sec009ComSecCompQueryCondition condition) { var ...
- linux下使用NFS挂载文件系统
转自linux如何使用NFS挂载文件系统 设备:一台服务器和一台客户端,这里我们把装在PC机上的RedHat作为服务器,而客户端则是嵌入式linux开发板. 环境:开发板已启动,连接好串口和网线,串口 ...
- Network Wars
zoj2676:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1676 题意:给出一个带权无向图 ,每条边e有一个权 .求将点 ...
- ANDROID_MARS学习笔记_S04_008_用Listview、自定义adapter显示返回的微博数据
一.简介 运行结果 二.代码1.xml(1)activity_main.xml <?xml version="1.0" encoding="utf-8"? ...
- ANDROID_MARS学习笔记_S03_004_getAllProviders、LOCATIONLISTENER、getBestProvider
一.代码 1.xml(1)activity_main.xml <uses-permission android:name="android.permission.ACCESS_FINE ...
- Qt创建窗体的过程
版权声明 本文为原创作品,请尊重作者的劳动成果.转载必须保持文章完整性,并以超链接形式注明原始作者“ tingsking18”和 主站点地址,方便其他朋友提问和指正. QT源码解析(一) QT创建窗口 ...