图像题,没觉得有什么简单的办法,貌似可以用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的更多相关文章

  1. LN : leetcode 399 Evaluate Division

    lc 399 Evaluate Division 399 Evaluate Division Equations are given in the format A / B = k, where A ...

  2. [LeetCode] 399. Evaluate Division 求除法表达式的值

    Equations are given in the format A / B = k, where A and B are variables represented as strings, and ...

  3. 【leetcode】399. Evaluate Division

    题目如下: Equations are given in the format A / B = k, whereA and B are variables represented as strings ...

  4. 【LeetCode】399. Evaluate Division 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  5. [leetcode] 399. Evaluate Division

    我是链接 看到这道题,2个点和一个权值,然后想到图,但是leetcode就是这样,没给数据范围,感觉写起来很费劲,然后就开始用图来做,添加边的时候,注意正向边和反向变,然后查询的时候,先判断2个点是否 ...

  6. [Leetcode Week3]Evaluate Division

    Evaluate Division题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/evaluate-division/description/ Desc ...

  7. [LeetCode] Evaluate Division 求除法表达式的值

    Equations are given in the format A / B = k, where A and B are variables represented as strings, and ...

  8. Leetcode: Evaluate Division

    Equations are given in the format A / B = k, where A and B are variables represented as strings, and ...

  9. [Swift]LeetCode399. 除法求值 | Evaluate Division

    Equations are given in the format A / B = k, where A and B are variables represented as strings, and ...

随机推荐

  1. JavaScript学习总结【12】、JS AJAX应用

    1.AJAX 简介 AJAX(音译为:阿贾克斯) = Asynchronous JavaScript and XML(异步的 JavaScript 和 XML),是指一种创建交互式网页应用的网页开发技 ...

  2. Yii render和renderPartial的区别

    以下由我们在信易网络公司开发项目的时候终结出的一些经验 在进行页面输出渲染的时候. 1.render 输出父模板的内容,将渲染的内容,嵌入父模板.|2.renderPartial 则不输出父模板的内容 ...

  3. C++ 11 笔记 (三) : auto

    我真的不是标题党... 虽然大一上学期学C语言基础时就学了auto关键字了,而且还是跟static和register两个关键字打包学的,但是.. 猜的没错,C++11这货又给auto加新功能了,在 C ...

  4. AOP和IOC理解

    在百度上看到一篇很有意思的文章,是对AOP的一种解释,如下:(摘自:百度文库的 AOP和IOC最容易理解的说明(Spring知识小计)): IOC,依赖倒置的意思, 所谓依赖,从程序的角度看,就是比如 ...

  5. IOS7,做为开发者,你需要知道的变更

    IOS7即将发布,那么我们需要做些什么呢? 升级你的程序Icon至 120*120 更新一张包含状态栏大小的闪屏图片 还有些什么东西呢? IOS7中需要使用更加扁平化的设计,所以BUTTON的图片,边 ...

  6. Codeforces Round #197 (Div. 2) : B

    也是水题一个,不过稍微要细心点.... 贴代码: #include<iostream> using namespace std; long long n,m; ; int main() { ...

  7. crontab 中 python(cx_Oracle)脚本执行时需要用户环境变量,怎么办??

    import cx_Oracle Traceback (most recent call last): File "", line 1, in ? ImportError: lib ...

  8. CAS单点登录配置[5]:测试与总结

    终于要结束了... 测试 1 我们同时打开Tomcat6和Tomcat7,如果报错请修改. 打 开浏览器,输入http://fighting.com/Client1,进入CAS登录界面,这里我们先输入 ...

  9. nginx优化 突破十万并发(转)

    一.一般来说nginx 配置文件中对优化比较有作用的为以下几项: 1. worker_processes 8; nginx 进程数,建议按照cpu 数目来指定,一般为它的倍数 (如,2个四核的cpu计 ...

  10. Google 发布 Android 性能优化典范

    2015年伊始,Google发布了关于Android性能优化典范的专题, 一共16个短视频,每个3-5分钟,帮助开发者创建更快更优秀的Android App.课程专题不仅仅介绍了Android系统中有 ...