图像题,没觉得有什么简单的办法,貌似可以用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. 【转】关于oracle with as用法

    原文链接:关于oracle with as用法 with as语法–针对一个别名with tmp as (select * from tb_name) –针对多个别名with   tmp as (se ...

  2. grub命令来引导linux

    由于对linux系统的好奇,想按在机器上玩玩.昨天忙活了一晚上,最终才把linux安装好.但高兴的有点太早了,我还以为进linux就像进 windows那么简单哪,没有想到却蹦出来一个引导命令(gru ...

  3. 使用GetLogicalDriveStrings获取驱动器根路径

    使用GetLogicalDriveStrings获取驱动器根路径,并使用自定义的GetDriveInfo函数获取驱动器的属性. VS2012 + win7 x64下调试通过. #include < ...

  4. Win7 + VS2015 + CMake3.6.1-GUI编译库

    CMake生成Unicode版本VC工程 Just add this line in your top CMakeLists.txt file:     add_definitions(-DUNICO ...

  5. 股票API

    实时股票数据接口大全 股票数据的获取目前有如下两种方法可以获取:1. http/javascript接口取数据2. web-service接口 1.http/javascript接口取数据 1.1Si ...

  6. sql 判断一个表的数据不在另一个表中

    SELECT a.* FROM a LEFT JOIN b ON a.key = b.key WHERE (b.key IS NULL) end as flag from a select id fr ...

  7. sqltext sqlarea

    sqltext 中sql 有完整的sql, sqlarea没有

  8. angular2 学习笔记 ( Router 路由 )

    参考 : https://angular.cn/docs/ts/latest/guide/router.html#!#can-activate-guard https://angular.cn/doc ...

  9. 移动应用产品开发-android开发(一)

    最近公司希望增添移动开发业务,进行移动互联网开发的调研及产品需求调研. 我主要负责技术解决方案的研究,从android开发开始学习.同时跟经理一起与其他部门同事沟通了解移动开发方面的需求. 在了解an ...

  10. 使AspNetPager控件中文显示分页信息

    在日常的编程过程中,很多学员对于使AspNetPager控件中文显示分页信息不是很清楚,本文将由达内的老师为各位学员介绍一下使AspNetPager控件中文显示分页信息的内容. AspNetPager ...