https://leetcode.com/problems/evaluate-division/

public class Solution {
private Map mp;
private class Item {
public String str;
public double prop;
public Item(String s, double p) {
str = s;
prop = p;
}
} public double[] calcEquation(String[][] equations, double[] values, String[][] queries) {
mp = new HashMap(); for (int i=0; i<values.length; i++) {
String str1 = equations[i][0];
String str2 = equations[i][1];
double val = values[i]; List lt = (List)mp.remove(str2);
if (lt == null) {
lt = new ArrayList();
}
Item itm = new Item(str1, val);
lt.add(itm);
mp.put(str2, lt); lt = (List)mp.remove(str1);
if (lt == null) {
lt = new ArrayList();
}
itm = new Item(str2, 1/val);
lt.add(itm);
mp.put(str1, lt);
} double []ret = new double[queries.length];
Set st = new HashSet();
Queue qe = new LinkedList(); Iterator itr;
List lt;
Item baseItm; for (int i=0; i<queries.length; i++) {
st.clear();
qe.clear(); double dret = -1;
String str1 = queries[i][0];
String str2 = queries[i][1]; qe.offer(new Item(str2, 1));
st.add(str2); while ((baseItm = (Item)qe.poll()) != null) { lt = (List)mp.get(baseItm.str);
if (lt == null) {
continue;
} itr = lt.iterator(); while (itr.hasNext()) { Item itmm = (Item)itr.next();
if (itmm.str.equals(str1)) {
dret = itmm.prop * baseItm.prop;
break;
} if (st.contains(itmm.str)) {
continue;
}
Item newItm = new Item(itmm.str, itmm.prop*baseItm.prop);
qe.offer(newItm);
st.add(itmm.str);
} if (dret != -1) {
break;
}
} ret[i] = dret;
} return ret; }
}

evaluate-division的更多相关文章

  1. [Leetcode Week3]Evaluate Division

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

  2. LN : leetcode 399 Evaluate Division

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

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

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

  4. Leetcode: Evaluate Division

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

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

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

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

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

  7. 【leetcode】399. Evaluate Division

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

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

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

  9. [leetcode] 399. Evaluate Division

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

  10. 399. Evaluate Division

    图像题,没觉得有什么简单的办法,貌似可以用Union Find来解. 感觉有2种思路,一种是先全部构建好每2个点的weight,然后直接遍历queires[][]来抓取答案. 一种是只构建简单的关系图 ...

随机推荐

  1. TradingView 自定义指标

    TradingView 支持自定义指标,不过是把你要定义的指标写成一个 JS 源文件(customIndex.js),放在图表库 static 文件夹下.自定义指标 JS 源代码模板如下: __cus ...

  2. sql中的if()和ifnull() 的用法和区别

    if() 把salary表中的女改成男,男改成女: update salary set sex = if( sex = '男','女','男'); if(true,a,b),  if(false,a, ...

  3. iuap

    2017.12 用友今年着力点往云平台发展,是时候整理一下思路 第一:iuap 第二:Linux 第三:财务会计业务入门 第四:NC节点视频教程--财务模块 2019年3月4日 all in iuap ...

  4. DataTable,List,Dictonary互转,筛选及相关写法

    1.创建自定义DataTable  /// 创建自定义DataTable(一) 根据列名字符串数组, /// </summary> /// <param name="sLi ...

  5. Metasploit小技巧

    meterpreter > 进入meterpreter之后即可进行一些相关木马操作了,下面是正式本章内容 首先可以查看帮助文档,命令“help”,挑常用操作来讲↓↓↓ ------------- ...

  6. C/C++ 之输入输出

    因为C++向下兼容C,所以有多种输入输出的方式,cin/cout十分简洁,但个人觉得不如scanf/printf来的强大,而且在做算法题时,后者运行速度也快些. scanf/printf #inclu ...

  7. HTTPClient实现免登陆请求(带cookie请求)

    背景: 使用httpClient请求某登录型网站,模拟一个操作,一般步骤一个httpclient模式登录->httpClient模拟操作: 此时发现,每次操作都需要进行一次登录,极其浪费时间,是 ...

  8. bzoj 1176

    收获: 1.min, max, sum, 属于判定,等询问是”对象对答案贡献独立“,即不需要其他对象就了能更新答案,并保证只要所有对象更新过答案,那么该答案就是正确的.大概这就是所谓的”修改独立“. ...

  9. 模板 SBT

    傻逼树模板 struct SBT{ const static int maxn = 1e5 + 15; int lft[maxn] , rht[maxn] , key[maxn] , s[maxn] ...

  10. 机器学习(1):Logistic回归原理及其实现

    Logistic回归是机器学习中非常经典的一个方法,主要用于解决二分类问题,它是多分类问题softmax的基础,而softmax在深度学习中的网络后端做为常用的分类器,接下来我们将从原理和实现来阐述该 ...