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的更多相关文章

  1. LN : leetcode 399 Evaluate Division

    lc 399 Evaluate Division 399 Evaluate Division Equations are given in the format A / B = k, where A ...

  2. [LeetCode] 399. Evaluate Division 求除法表达式的值

    Equations are given in the format A / B = k, where A and B are variables represented as strings, and ...

  3. [leetcode] 399. Evaluate Division

    我是链接 看到这道题,2个点和一个权值,然后想到图,但是leetcode就是这样,没给数据范围,感觉写起来很费劲,然后就开始用图来做,添加边的时候,注意正向边和反向变,然后查询的时候,先判断2个点是否 ...

  4. [LeetCode] Evaluate Division 求除法表达式的值

    Equations are given in the format A / B = k, where A and B are variables represented as strings, and ...

  5. 【LeetCode】399. Evaluate Division 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  6. Leetcode: 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] 150. Evaluate Reverse Polish Notation 计算逆波兰表达式

    Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...

  9. 【Leetcode】Evaluate Reverse Polish Notation JAVA

       一.问题描述 Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators ...

随机推荐

  1. ES5新增数组方法(2):map

    通过指定函数处理数组的每个元素,并返回处理后的数组. 1.计算数组中每个元素的平方 let arr = [1, 2, 3, 4, 5, 6]; let newArr = arr.map(item =& ...

  2. Python 3基础教程25-异常处理

    在Python中,异常处理,主要是try except语句,通常语法格式如下. try: 代码块1 except Exception as e: print(e) 代码2 接着前面读取CSV文件,如果 ...

  3. wampserver 安装后 启动失败的解决方法

    安装后启动, 显示 发生未知的异常 wampmanager.exe  .... 解决方法 === 其实下载页面说了,先下载 vc的运行库,页面上有链接, 他给的是vc10的,我按照做,失败 查了无数资 ...

  4. php处理三级分类数据

    <?php // 链接数据库 $link = mysqli_connect('localhost','root','root'); if($link == null){ exit; } mysq ...

  5. 修改maven远程仓库为阿里的maven仓库(复制)

    maven之一:maven安装和eclipse集成 maven作为一个项目构建工具,在开发的过程中很受欢迎,可以帮助管理项目中的bao依赖问题,另外它的很多功能都极大的减少了开发的难度,下面来介绍ma ...

  6. cmd端口占用查看和关闭端口

    cmd——回车,输入netstat -ano——回车,可以查看已占用的端口,记下端口的PID,然后打开任务管理器,点查看,选择列,勾选PID确定,找到对应的PID,结束进程,如果结束不了或者结束后还不 ...

  7. Nova Cell

    Nova Cell V2 详解 现在 ,OpenStack 在控制平面上的性能瓶颈主要在 Message Queue 和 Database . 尤其是 Message Queue , 随着计算节点的增 ...

  8. 利用npm安装/删除/发布/更新/撤销发布包

    利用npm安装/删除/发布/更新/撤销发布包 什么是npm? npm是javascript的包管理工具,是前端模块化下的一个标志性产物 简单地地说,就是通过npm下载模块,复用已有的代码,提高工作效率 ...

  9. 【题解】SHOI2001化工厂装箱员

    ————传送:洛谷P2530 这道题目还是挺简单的,状态也容易想到. 数据范围非常的小,所以即便是很多维度,复杂度也完全可以接受.定义状态:dp[i][a][b][c]为手上的货物拿到第i个时三种物品 ...

  10. BZOJ3456 城市规划 【多项式求逆】

    题目链接 BZOJ3456 题解 之前我们用分治\(ntt\)在\(O(nlog^2n)\)的复杂度下做了这题,今天我们使用多项式求逆 设\(f_n\)表示\(n\)个点带标号无向连通图数 设\(g_ ...