evaluate-division
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的更多相关文章
- [Leetcode Week3]Evaluate Division
Evaluate Division题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/evaluate-division/description/ Desc ...
- LN : leetcode 399 Evaluate Division
lc 399 Evaluate Division 399 Evaluate Division Equations are given in the format A / B = k, where A ...
- [LeetCode] Evaluate Division 求除法表达式的值
Equations are given in the format A / B = k, where A and B are variables represented as strings, and ...
- Leetcode: Evaluate Division
Equations are given in the format A / B = k, where A and B are variables represented as strings, and ...
- [Swift]LeetCode399. 除法求值 | Evaluate Division
Equations are given in the format A / B = k, where A and B are variables represented as strings, and ...
- [LeetCode] 399. Evaluate Division 求除法表达式的值
Equations are given in the format A / B = k, where A and B are variables represented as strings, and ...
- 【leetcode】399. Evaluate Division
题目如下: Equations are given in the format A / B = k, whereA and B are variables represented as strings ...
- 【LeetCode】399. Evaluate Division 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- [leetcode] 399. Evaluate Division
我是链接 看到这道题,2个点和一个权值,然后想到图,但是leetcode就是这样,没给数据范围,感觉写起来很费劲,然后就开始用图来做,添加边的时候,注意正向边和反向变,然后查询的时候,先判断2个点是否 ...
- 399. Evaluate Division
图像题,没觉得有什么简单的办法,貌似可以用Union Find来解. 感觉有2种思路,一种是先全部构建好每2个点的weight,然后直接遍历queires[][]来抓取答案. 一种是只构建简单的关系图 ...
随机推荐
- Python爬虫个人记录(二) 获取fishc 课件下载链接
参考: Python爬虫个人记录(一)豆瓣250 (2017.9.6更新,通过cookie模拟登陆方法,已成功实现下载文件功能!!) 一.目的分析 获取http://bbs.fishc.com/for ...
- .NET之类型转换
说起类型转换大家很容易的就会联想到将int类型转换成float类型或者是将double类型转转成int类型之类的转换.当然这可能是大多数人最先接触到的转换方式,也是最简单的转换方式.所谓转换就是从现有 ...
- 20172304 实验二 《Java面向对象程序设计》 实验报告
20172304 实验二 <Java面向对象程序设计> 实验报告 课程名称:<程序设计与数据结构> 学生班级:1723班 学生姓名:段志轩 学生学号:20172304 实验时间 ...
- CSUOJ 1979 古怪的行列式
Description 这几天,子浩君潜心研究线性代数. 行列式的值定义如下: 其中,τ(j1j2...jn)为排列j1j2...jn的逆序数. 子浩君很厉害的,但是头脑经常短路,所以他会按照行列式值 ...
- 洛谷——P2936 [USACO09JAN]全流Total Flow
题目描述 Farmer John always wants his cows to have enough water and thus has made a map of the N (1 < ...
- 新一代数据库TiDB在美团的实践
1. 背景和现状 近几年,基于MySQL构建的传统关系型数据库服务,已经很难支撑美团业务的爆发式增长,这就促使我们去探索更合理的数据存储方案和实践新的运维方式.而随着分布式数据库大放异彩,美团DBA团 ...
- Linux教程 - 管道和重定向
管道和重定向! 保持数据流动 介绍 在前两节中,我们看了一些可以为我们操作数据的过滤器.在本节中,我们将看到我们如何将它们结合在一起来执行更强大的数据操作. 本节涉及一些阅读.即使这些机制及其 ...
- Rob Pike:我得到的最佳编程建议
Rob Pike:我得到的最佳编程建议 Rob Pike,目前谷歌公司最著名的软件工程师之一,曾是贝尔实验室Unix开发团队成员,Plan9操作系统开发的主要领导人,Inferno操作系统开发的主要领 ...
- 特殊字符导致jquery-mobile 挂起(firefox控制台报错 malformed URI sequence)
同事遇到一个问题,刷新页面导致页面挂起,浏览器控制台报错 malformed URI sequence, 经排查发现是引用jquery-mobile js引起的问题, 有一些中文参数在url中,当页面 ...
- [SQL]196. Delete Duplicate Emails
Write a SQL query to delete all duplicate email entries in a table named Person, keeping only unique ...