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. CentOS 升级Python3.X和pip3

    目的:实现python3 and python2 共存,pip2 and pip3共存 一.安装依赖 yum install openssl-devel -y yum install zlib-dev ...

  2. win7 fiddler报“Creation of the root certificate was not successful”的问题

    cd "C:\Program Files (x86)\Fiddler2" makecert.exe -r -ss my -n "CN=DO_NOT_TRUST_Fiddl ...

  3. Python并发编程系列之多进程(multiprocessing)

    1 引言 本篇博文主要对Python中并发编程中的多进程相关内容展开详细介绍,Python进程主要在multiprocessing模块中,本博文以multiprocessing种Process类为中心 ...

  4. 虚拟多Mac地址工具Multimac

    虚拟多Mac地址工具Multimac   Mac地址采用唯一标识标记网络的各种设备.在同一个时间内,Linux系统中的网卡只能使用一个Mac地址.在渗透测试中,为了隐藏自己的身份,往往需要以不同的Ma ...

  5. JavaScript数据类型学习脑图:

  6. Alpha 冲刺报告5

    组长:吴晓晖 今天完成了哪些任务: 将服务端程序基本部署在阿里云上,还未进行测试 完成了手写记录的代码实现 处理团队问题 为明天的编程任务做准备 展示GitHub当日代码/文档签入记录: 明日计划: ...

  7. Windows Phone background Audio 后台音频

    Windows Phone 后台音频的确不是什么新鲜的话题了,但发现目前在WP平台的音频播放应用多多少少会有一些瑕疵,所以在此给大家在此介绍下这个功能给有需要的朋友们. 首先介绍下我们的应用在后台播放 ...

  8. JavaScript将具有父子关系的原始数据格式化成树形结构数据(id,pid)

    前几天遇到一个树型组件(类似树形菜单)数据格式化的问题,由于后台把原始查询的数据直接返回给前端,父子关系并未构建,因此需要前端JS来完成,后台返回的数据和下面的测试数据相似. var data=[ { ...

  9. .vs目录有什么用?

    写这篇博文的目的就是方便后来者能够在百度里轻松搜到. 反正我找了半天没找到关于.vs目录的介绍,最后还是在同事的帮助下才找到的. 参考地址:https://developercommunity.vis ...

  10. INTEL SSD SMART 性能 E8 E9 转

    如题今天研究了一下怎么看Intel SSD Toolbox里边显示的Smart信息.首先说,查看Intel SSD smart信息最好最方便的当然就是自家的工具箱,用其他工具查看不是不可以,但是很多数 ...