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 ...
随机推荐
- HTML标签总结
HTML 基本文档 <!DOCTYPE html> <html> <head> <title>文档标题</title> </head& ...
- simplexml 使用实例
搞了几天php处理xml文件,终于有点头绪,记录下来分享一下.simplexml 是php处理xml文件的一个方法,另一个是dom处理,这里只说simplexml.目前php处理xml用的比较多,比较 ...
- 什么是php?以及mysqlnd与libmysqlclient
今天想彻底搞清楚php与mysql的关系,于是在php官方网站(http://php.net/manual/en/mysqli.installation.php) 看了一下mysqli,mysql.感 ...
- Omnithreadlibary学习(2)-IOmniTask异步执行
1.使用 procedure Parallel.Async(task: TProc; taskConfig: IOmniTaskConfig); 匿名委托访问网站 program main; {$AP ...
- linux服务器监控流量sh脚本
服务器可能经常遇到服务器出带宽跑满,不知如何查询被哪个进程占用的情况,有一款开源的英文软件iftop功能比较强大可以查询相关信息,可能刚接触linux系统的朋友不太会使用,在此写了一个功能比较简单无需 ...
- 问自己----也是自己该怎么走的路(phper)
1.首先看了PHP的源码API函数,对于许多口水仗的争论一笑而过,只是停留在脚本级别上的什么效率,安全...之争完全就是无稽之谈,没有深入理解API,所有的争论都是臆测和不科学的态度.你做了吗? 2. ...
- armv7a-mediatek451_001_vfp-linux-gnueabi-gcc: directory: No such file or directory 编译error
release/vm_linux/output/hisense_android/mt5399_cn_android_JB/rel/obj/oss/source/arm_mali_ko/mali400- ...
- iOS 静态库中使用宏定义区分iPhone模拟器与真机---备用
问题描述 一般项目中,可以使用宏定义来判断模拟器还是真机,这无疑是有效的. #if TARGET_IPHONE_SIMULATOR #define SIMULATOR 1 #elif TARGET_O ...
- android SurfaceView绘制 重新学习--切图clipRect详解
解释都在代码注释中: public class SampleView extends View { private Paint mPaint; private Path mPath; public S ...
- 在游戏中使用keybd_event的问题
转自在游戏中使用keybd_event的问题 今天发现在游戏中,keybd_event不能使用,结果发现游戏是使用directinput实现读取键盘的,关键还是扫描码的问题,我抄了一段老外的代码,经测 ...