LN : leetcode 399 Evaluate Division
lc 399 Evaluate Division
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<double>& values, vector<pair<string, string>> queries
, where equations.size() == values.size()
, and the values are positive. This represents the equations. Return vector<double>
.
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.
DFS Accepted##
这个星期老师讲了图的分解以及DFS算法,所以我就在leetcode上找了一道相关的题目。问题的关键在于创建一个有向图,对于每一个字符串结点来说,应用map<string, map<string,double>> node
这样的数据结构来存储每个有向边是很适应该问题的。根据equations和values中的值来创建有向边,正向为values[i],反向为1/values[i]。
创建完有向图后,利用DFS对每个query从起点出发进行搜索,每次都乘以该条路径上的值,若两点间不能连通或点不在node范围之内,则返回0,最终输出-1.0。
class Solution {
public:
vector<double> calcEquation(vector<pair<string, string>> equations, vector<double>& values, vector<pair<string, string>> queries) {
vector<double> ans;
map<string, map<string,double>> node;
for (int i = 0; i < values.size(); i++) {
node[equations[i].first].insert(make_pair(equations[i].second, values[i]));
node[equations[i].second].insert(make_pair(equations[i].first, 1/values[i]));
}
for (auto i : queries) {
set<string> s;
double num = myfind(i.first, i.second, node, s);
if (num) ans.push_back(num);
else ans.push_back(-1.0);
}
return ans;
}
double myfind(string first, string second, map<string, map<string,double>> &node, set<string> &s) {
if (node[first].find(second) != node[first].end()) return node[first][second];
for (auto i : node[first]) {
if (s.find(i.first) == s.end()) {
s.insert(i.first);
double num = myfind(i.first, second, node, s);
if (num) return i.second*num;
}
}
return 0;
}
};
LN : leetcode 399 Evaluate Division的更多相关文章
- [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 Week3]Evaluate Division
Evaluate Division题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/evaluate-division/description/ Desc ...
- 【LeetCode】399. Evaluate Division 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【leetcode】399. Evaluate Division
题目如下: Equations are given in the format A / B = k, whereA and B are variables represented as strings ...
- 399. Evaluate Division
图像题,没觉得有什么简单的办法,貌似可以用Union Find来解. 感觉有2种思路,一种是先全部构建好每2个点的weight,然后直接遍历queires[][]来抓取答案. 一种是只构建简单的关系图 ...
- [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 ...
- [LeetCode] 150. Evaluate Reverse Polish Notation 计算逆波兰表达式
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
随机推荐
- 调整JVM内存大小
首次运行公司项目,出现了内存溢出,具体出现java.lang.OutOfMemoryError: PermGen space和java.lang.OutOfMemoryError:GC overhea ...
- 所有的异常都要使用try catch 语句捕获?
在开发应用程序过程中必须检测代码可能发生的错误并进行正确的处理,这个在理想的情况下,应用程序中的每行 代码都按照预想的执行,要用到的每种资源总是可以利用,但是在实际的开发过程中,写代码难免会出错,或是 ...
- 深入理解windows系统内的GMT和时区
http://www.itshanghai.net/technology/wdzl_windowsxp/ UTC(Universal Time Coordinated)是通用协调时,这两者几乎是一 ...
- 【SQL Server 学习系列】-- 随机生成日期时间的SQL脚本
DECLARE @dt1 DATETIME,@dt2 DATETIME,@a BIGINT,@b BIGINT SET @dt1='2010-01-01'--开始日期 SET @dt2='2010-0 ...
- Java RMI之HelloWorld程序以及相关的安全管理器的知识
Java RMI 指的是远程方法调用 (Remote Method Invocation).它是一种机制,可以让在某个 Java 虚拟机上的对象调用还有一个 Java 虚拟机中的对象上的方法.可以用此 ...
- 排列组合(permutation)系列解题报告
本文解说4道关于permutation的题目: 1. Permutation:输出permutation--基础递归 2. Permutation Sequence: 输出字典序排列的第k个permu ...
- pojWindow Pains(拓扑排序)
题目链接: 啊哈哈,点我点我 题意: 一快屏幕分非常多区域,区域之间能够相互覆盖,要覆盖就把属于自己的地方所有覆盖. 给出这块屏幕终于的位置.看这块屏幕是对的还是错的.. 思路: 拓扑排序,这个简化点 ...
- iOS8使用TouchID
iOS8新增了LocalAuthentication框架,用于TouchID的授权使用.亲測,眼下须要用户的设备支持指纹识别并已设置锁屏,并且实际測试过程中反馈比較慢.不能直接跟第三方账号passwo ...
- c++命令提示符窗体下打印指定大小的菱形代码
c++命令提示符窗体下打印指定大小的菱形代码 VS2010下,新建空项目.加入源文件,将代码粘贴进去就能够了. 通过改maxRows值的大小,能够控制菱形的大小 #include <stdio. ...
- openstack页面自己定义插件使用具体解释(django、ajax、post)(zTree为例)
感谢朋友支持本博客,欢迎共同探讨交流.因为能力和时间有限,错误之处在所难免,欢迎指正! 如有转载.请保留源作者博客信息. Better Me的博客:blog.csdn.net/tantexian 如需 ...