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 +, -, ...
随机推荐
- FTPUtil工具类
package com.xxx.common.util; import java.io.File; import java.io.FileOutputStream; import java.io.IO ...
- MongoDB学习day02--数据库增删改查
(window系统,在cmd命令提示符中使用) 一.数据库使用 管理mongodb数据库:mongo,连接本地数据库,或mongo 127.0.0.1:27017,连接其他服务器:mongo ip: ...
- MongoDB小结21 - find【游标】
数据库使用游标来控制find的执行结果. 客户端对游标的实现通常能够对最终结果进行有效控制. 可以限制结果的数量,略过部分结果,对任意方向任意键的组合对结果进行排序,或者去执行一些功能强大的操作. 我 ...
- iOS 自己主动释放手动释放混编
当项目为手动释放时,Build Settings中,Objective-c Automatic Reference Conting 为YES 时,想要手动管理一些文件,在CompileSources中 ...
- MyEclipse 8.5安装Aptana
Aptana简单介绍 Aptana是一个很强大,开源,专注于JavaScript的Ajax开发IDE它的特性包含: 1.JavaScript,JavaScript函数,HTML,CSS语言的Co ...
- 斜率优化专题1——bzoj 1597 [Usaco2008 Mar] 土地购买 题解
转载请注明:http://blog.csdn.net/jiangshibiao/article/details/24387147 [原题] 1597: [Usaco2008 Mar]土地购买 Time ...
- ubuntu下apache+mysql+php+mysql等之webserver搭建
相信非常多人跟我一样,想搭建一个自己的webserver.网上资料非常多.可是因为版本号的区别,总是存在依照一个教程来做无法全然实现的问题.近期我也折腾了好几天,google不能用,仅仅能百度,真想说 ...
- 免安装版TOMCAT配置及问题解决方法
前言 本文将介绍下面几点内容: 1.Tomcat的配置过程 2.启动startup过程中遇到的问题的解决 3.假设遇到本文中没有提到的问题怎样解决 配置 计算机右击->属性->高级系统设置 ...
- HttpUtils 发送http请求工具类
import java.io.IOException; import java.io.UnsupportedEncodingException; import java.net.URISyntaxEx ...
- c#生成AVI自动设置压缩格式,不调用AVISaveOptions
工作中遇到生成AVI视频的项目,代码中会调用AVISaveOptions来设置压缩格式,针对单个文件还好说,但是批量生成视频的时候,每一个都要设置格式, 体验不是很好,经过查询资料问题得到解决 最开始 ...