[Leetcode Week3]Evaluate Division
Evaluate Division题解
原创文章,拒绝转载
题目来源:https://leetcode.com/problems/evaluate-division/description/
Description
Equations are given in the format A / B = k, where A and B are variables represented as strings, and k is a real number (floating point number). Given some queries, return the answers. If the answer does not exist, return -1.0.
Example
Given a / b = 2.0, b / c = 3.0.
queries are: a / c = ?, b / a = ?, a / e = ?, a / a = ?, x / x = ? .
return [6.0, 0.5, -1.0, 1.0, -1.0 ].
The input is: vector<pair<string, string>> equations, vector& values, vector<pair<string, string>> queries , where equations.size() == values.size(), and the values are positive. This represents the equations. Return vector.
According to the example above:
equations = [ ["a", "b"], ["b", "c"] ],
values = [2.0, 3.0],
queries = [ ["a", "c"], ["b", "a"], ["a", "e"], ["a", "a"], ["x", "x"] ].
The input is always valid. You may assume that evaluating the queries will result in no division by zero and there is no contradiction.
Solution
class Solution {
private:
map<pair<string, string>, double> graph;
map<string, bool> isVisited;
public:
vector<double> calcEquation(vector<pair<string, string>> equations, vector<double>& values, vector<pair<string, string>> queries) {
int i;
vector<double> resultVec;
for (i = 0; i < equations.size(); i++) {
graph[equations[i]] = values[i];
graph[make_pair(equations[i].second, equations[i].first)] = 1.0 / values[i];
isVisited[equations[i].first] = isVisited[equations[i].second] = false;
}
for (auto& q: queries) {
if (isVisited.find(q.first) == isVisited.end() ||
isVisited.find(q.second) == isVisited.end()) {
resultVec.push_back(-1.0);
} else if (q.first == q.second) {
resultVec.push_back(1.0);
} else {
resultVec.push_back(dfs_cal(q));
}
}
return resultVec;
}
double dfs_cal(pair<string, string> p) {
double result = -1.0;
isVisited[p.first] = true;
try {
result = graph.at(p);
} catch (const out_of_range& err) {
for (auto& edge: graph) {
if (p.first == edge.first.first && !isVisited[edge.first.second]) {
if ((result = dfs_cal(make_pair(edge.first.second, p.second))) > 0) {
result *= edge.second;
break;
}
}
}
}
isVisited[p.first] = false;
return result;
}
};
解题描述
这道题初步的想法就是通过以字符串作为顶点的标识,用map模拟构建一个邻接矩阵。然后对于给定的查询,使用DFS求得路径。总的来说没有坑点,不过可能由于使用的STL较多,运行的时间还是相对比较长。查看了一下题目的Discuss,发现使用并查集算法来解决的话耗费时间较少,而且也不难理解。
[Leetcode Week3]Evaluate Division的更多相关文章
- LN : leetcode 399 Evaluate Division
lc 399 Evaluate Division 399 Evaluate Division Equations are given in the format A / B = k, where A ...
- [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
我是链接 看到这道题,2个点和一个权值,然后想到图,但是leetcode就是这样,没给数据范围,感觉写起来很费劲,然后就开始用图来做,添加边的时候,注意正向边和反向变,然后查询的时候,先判断2个点是否 ...
- [LeetCode] 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 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- Leetcode: 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] 150. Evaluate Reverse Polish Notation 计算逆波兰表达式
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
- 【Leetcode】Evaluate Reverse Polish Notation JAVA
一.问题描述 Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators ...
随机推荐
- PyTorch深度学习计算机视觉框架
Taylor Guo @ Shanghai - 2018.10.22 - 星期一 PyTorch 资源链接 图像分类 VGG ResNet DenseNet MobileNetV2 ResNeXt S ...
- tomcat web.log 系统日志记录文件过大问题修改
目前各系统都是记录所有的日志,产生日志文件太大,按照如下设置修改log4j.properties文件:其中橙色部分为系统名称,例如water-scada系统,名称可以为scada. #Sun Jun ...
- Hibernate 查询,返回结果设置到DTO
1:原生sql的查询,返回结果设置到DTO: Query query = sessionFactoryRtData.getCurrentSession().createSQLQuery(hql.toS ...
- URAL 1932 The Secret of Identifier(容斥)
Description Davy Jones: You've been captain of the Black Pearl for 13 years. That was our agreement. ...
- DP入门(1)——数字三角形问题
一.问题描述 如上图所示,有一个由非负整数组成的三角形,第一行只有一个数,除了最下行之外每个数的左下方和右下方各有一个数.现请你在此数字三角形中寻找一条从首行到最下行的路径,使得路径上所经过的数字之和 ...
- coredump分析
首先通过命令 gdb freeswitch core.60954进入gdb. 这里freeswitch 是产生coredump的可执行应用,core.60954是应用产生的coredump文件. 然后 ...
- 子查询 做where条件 做 from的临时表 ,做select的一个字段 等
子查询 做where条件 做 from的临时表 ,做select的一个字段 等
- 【bzoj3813】奇数国 线段树
题目描述 给出一个长度为n的序列,每个数都可以由前60个质数的乘积表示,初始每个数都是3.支持两种操作:(1)修改一个数 (2)查询一段区间内所有数的乘积的欧拉函数值模19961993. 输入 第一行 ...
- HTML5<canvas>标签:使用canvas元素在网页上绘制四分之一圆(3)
前几天自己做了个四分之一的圆,放到手机里面测试.效果不是很好.于是今天通过查资料,找到了canvas.自己研究了一天,发现可以使用canvas画圆.代码如下: <!doctype html> ...
- BZOJ4602 SDOI2016齿轮(搜索)
dfs一遍给每个齿轮随便标个值看是否矛盾就行了. #include<iostream> #include<cstdio> #include<cmath> #incl ...