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. Ubuntu 17.04 搭建 NodeJS

    可以在云主机上执行以下的命令: apt-get update apt-get install -y python-software-properties software-properties-com ...

  2. synchoronized和lock区别

    synchoronized是JVM的内置锁,而lock是Java代码实现的.lock是sync对的扩展,完全可以替代后者.lock可以重入,允许同一个线程连续多次获得同一把锁.其次,lock独有的功能 ...

  3. 跑对抗样本库 CleverHans 的例子时,遇到的问题

    环境:Ubuntu+TensorFlow 首先是GPU被其他人占用了,怎么也跑不起来最简单的TensorFlow小例子. 所以先学会如何查看显卡使用情况,转去使用其他空闲显卡. Linux查看Nvid ...

  4. Chrome谷歌浏览器拓展组件的2种快速安装方法(.crx)

    谷歌浏览器拓展有至少2种安装方法,现在简单的介绍下. 第一种.当然是进入谷歌官方的应用商店直接安装 这种方法简单快捷,而且官方支持度够高,唯一的缺点是大陆用户需要“FQ”. 谷歌拓展组件应用商店地址: ...

  5. 王彪-20162321-Java程序设计与数据结构2nd-第十周学习总结

    学习目标 讨论有向图和无向图 定义带权图并讨论它们的应用 定义图的广度优先遍历和深度优先遍历 定义最小生成树 讨论图的实现策略 书中图的基本定义 图是由结点及结点间的连接组成的,结点称为顶点,结点间的 ...

  6. C++11中的raw string literals

    作为一名C++书看得少得可怜的新手,我一直没有勇气去系统地学习一下C++ 11添加的新特性.不过,平日里逛论坛,阅读大犇们的博客,倒是了解了一些.比如,这个帖子: 如何绕过g++ 4.8.1那个不能在 ...

  7. 监控 Linux 系统的 7 个命令行工具

    监控 Linux 系统的 7 个命令行工具: " 深入 关于Linux最棒的一件事之一是你能深入操作系统,来探索它是如何工作的,并寻找机会来微调性能或诊断问题.这里有一些基本的命令行工具,让 ...

  8. wikioi 1048 石子归并

    dp[i][j]=min(dp[i][j],dp[i][k],dp[k+1][j]+sum[i][j]); 表示i-j的最小合并代价. #include <iostream> #inclu ...

  9. Bootstrap_CSS概览

    在这一章中,我们将讲解 Bootstrap 底层结构的关键部分,包括我们让 web 开发变得更好.更快.更强壮的最佳实践. HTML 5 文档类型(Doctype) Bootstrap 使用了一些 H ...

  10. java8函数式接口小例子

    // Function<T, R> -T作为输入,返回的R作为输出 Function<String,String> function = (x) -> {System.o ...