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. QC的使用学习(二)

    今日学习清单: 1.Quality  Center中左上角选项中(QC 10.0中文版)工具菜单下的自定义中的几个内容,有:用户属性.组.项目用户.模块访问.需求类型.项目列表等.用户属性打开后是对当 ...

  2. jmeter实例,如果有说明错误,请各位大神批评

    首先我们打开jmeter,今天录制的脚本的是获取QQ头像,找了好久才找到可以免费试用的接口,如果有什么错误的地方,欢迎大家提出来,我会及时修改,也给自己一次进步的机会,希望大家不吝赐教!!!如果有什么 ...

  3. 从底层带你理解Python中的一些内部机制

    下面博文将带你创建一个字节码级别的追踪API以追踪Python的一些内部机制,比如类似YIELDVALUE.YIELDFROM操作码的实现,推式构造列表(List Comprehensions).生成 ...

  4. CentOS7 Zabbix4.0环境下的安装和配置实例

    1.安装准备 Zabbix4.0对基础架构有一定的要求,对的英文尤其PHP状语从句:MySQL: 类型 内容 服务端运行环境 Linux和PHP与Web服务器和数据库 服务端操作系统 CentOS7. ...

  5. 剑指offer-数值的整数次方12

    class Solution: def Power(self, base, exponent): # write code here if base==0: return 0 if exponent= ...

  6. 【解决】Node JS Error: ENOENT

    The Node Beginner Book 书中的实例代码当上传图片时会报Error: ENOENT, 原因:图片默认会选择系统的缓存文件夹下,在windows下无权访问C盘,所以就报错了.. 解决 ...

  7. idea 快捷键(复制)

    Ctrl+Shift + Enter,语句完成“!”,否定完成,输入表达式时按 “!”键Ctrl+E,最近的文件Ctrl+Shift+E,最近更改的文件Shift+Click,可以关闭文件Ctrl+[ ...

  8. 深度优先搜索(DFS),逃离迷宫

    [原创] 今天来说说深度优先搜索,深度优先是图论中的内容,大意是从某一点出发,沿着一个方向搜索下去,并伴随着有回退的特点,通常用来判断某一解是否存在,不用来寻找最优解:这里来看一个非常有意思的题目: ...

  9. SpringMVC-01-宏观上把握SpringMVC框架

    springmvc是一个基于mvc的web框架,是spring框架的一个模块,所以springmvc和spring无需通过中间整合层进行整合.我们先来看下spring的一个架构模型,看springmv ...

  10. To Chromium之浏览器外框UI

    先不去管那些webkit,V8 engine, Parser, security,IPC... 先来看看Chromium的外框UI是那些code负责的,如果自己可以定制化一下,应该蛮好玩的. TBD. ...