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. JDK源码分析(三)——HashMap 上(基于JDK7)

    目录 HashMap概述 内部字段及构造方法 存储元素 扩容 取出元素 删除元素 判断 总结 HashMap概述   前面我们分析了基于数组实现的ArrayList和基于双向链表实现的LinkedLi ...

  2. java中的dao模式

    java中Dao模式   什么是DAO   1.Data Access Object(数据存取对象) 2.位于业务逻辑和持久化数据之间 3.实现对持久化数据的访问 DAO模式的作用 1隔离业务逻辑代码 ...

  3. 配置k8s dns

    DNS (domain name system),提供域名解析服务,解决了难于记忆的IP地址问题,以更人性可读可记忆可标识的方式映射对应IP地址. Cluster DNS扩展插件用于支持k8s集群系统 ...

  4. scrapy运行机制

    Scrapy主要包括了以下组件: 引擎(Scrapy)用来处理整个系统的数据流, 触发事务(框架核心) 调度器(Scheduler)用来接受引擎发过来的请求, 压入队列中, 并在引擎再次请求的时候返回 ...

  5. [BZOJ5305][HAOI2018]苹果树(DP)

    首先注意到每种树都是等概率出现的,于是将问题转化成计数求和问题. f[n]表示所有n个点的树的两两点距离和的总和. g[n]表示所有n个点的树的所有点到根的距离和的总和. h[n]表示n个点的树的可能 ...

  6. 给HTML初学者的三十条最佳实践

    Nettuts +运营最困难的方面是为很多技能水平不同的用户提供服务.如果我们发布太多高级教程,我的新手用户将无法从中受益.相反也是如此.我们尽我们最大的努力,但如果你觉得你被忽略了请联系我们.这个网 ...

  7. JAVA读入挂

    队友扒的uwi的读入挂,非常强,再也不用担心java比C++慢了-- import java.util.*; import java.math.*; import java.io.ByteArrayI ...

  8. numpy中的random函数

    1:rand rand(d0, d1, ..., dn)    Random values in a given shape.    Create an array of the given shap ...

  9. 推荐C#网站、书籍、资源

    推荐博客: 极简的随笔 http://www.cnblogs.com/guwei4037/p/3499135.html 见证大牛成长之路的专栏 http://blog.csdn.net/shanyon ...

  10. MySql篇

    CentOS6下通过yum安装的MySQL是5.1版的,比较老,所以就想通过源代码安装高版本的5.6.26. 一:卸载旧版本 使用下面的命令检查是否安装有MySQL Server rpm -qa | ...