图像题,没觉得有什么简单的办法,貌似可以用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. firefox下对ajax的onreadystatechange的支持情况分析及解决

    一.问题: var xmlHttp; function savecarttodata(){ createXMLHttpRequest(); var rndcode = new Date().getTi ...

  2. java 面向对象——进度1

    面向对象:1,面向对象和面向过程思想.        面向对象强调的是对象实例.    面向过程强调的是动作.    对象将动作进行封装.    在问题领域中,我们先去找的都是涉及的对象,    然后 ...

  3. IE11的CSS兼容性问题

    最近测试给了我一大堆BUG,一瞅发现全是IE11的.吐槽一下这个浏览器真的比较特立独行.很多默认的样式跟别的浏览器不同,而且最明显的一点应该是padding左右内边距往往比别的浏览器大了一倍.但是当需 ...

  4. 针对IE的CSS hack 全面 实用

    .all IE{property:value\9;} .gte IE 8{property:value\0;} .lte IE 7{*property:value;} .IE 8/9{property ...

  5. python类库26[web2py之基本概念]

    一 web2py的应用的执行环境Models,Controllers和views所在的执行环境中,以下对象已经被默认地导入: Global Objects:  request,response,ses ...

  6. C++的数据类型

    C++的数据类型 计算机处理的对象是数据,而数据是以某种特定的形式存在的(例如整数.浮点数.字符等形式). 不同的数据之间往往还存在某些联系(例如由若干个整数组成一个整数数组).数据结构指的是数据的组 ...

  7. Struts, Namespace用法

    最近在用SSH框架做一个项目,在使用Struts 的namespace时遇到不少问题,现在就对struts namespace 做一个简单的介绍吧.(本文从项目结构展开叙述) (第1次写博客, 写的不 ...

  8. [HDOJ 5212] [BestCoder Round#39] Code 【0.0】

    题目链接:HDOJ - 5212 题目分析 首先的思路是,考虑每个数对最终答案的贡献. 那么我们就要求出:对于每个数,以它为 gcd 的数对有多少对. 显然,对于一个数 x ,以它为 gcd 的两个数 ...

  9. 自定义滚轮效果选择器spinnerwheel的使用总结

    项目中有使用到像IOS滚轮效果的选择时间或数字的组件:android-spinnerwheel github地址:https://github.com/ai212983/android-spinner ...

  10. listview异步加载sd卡图片

    package com.example.gridview; import java.io.File; import java.io.FileOutputStream; import java.io.I ...