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 +, -, ...
随机推荐
- java.lang.NoClassDefFoundError: Could not initialize class异常处理
借鉴:http://blog.csdn.net/sleepdancer/article/details/9207425 static { InputStream in = XXX.class.getR ...
- 模拟用户点击弹出新页面不会被浏览器拦截_javascript技巧
原文:http://www.html5cn.com.cn/article/zxzx/3195.html 相信用过window.open的小伙伴们都遇到过被浏览器拦截导致页面无法弹出的情况:我们换下思路 ...
- 自己构建的Lumbda表达式
自己构建的Lumbda表达式 定义接口: package com.stono.lambda; public interface Add { public void add(int x, int y); ...
- yarn 和 npm 的区别
npm 与 yarn 命令对比 npm yarn npm install yarn install (N/A) yarn install --flat (N/A) yarn install --har ...
- 【项目实战】---使用ajax完毕username是否存在异步校验
小伙伴在上网的时候.须要下载或者观看某些视频资料,更或者是在逛淘宝的时候.我们都须要注冊一个用户,当我们填写好各种信息,点击确定的时候.提示username已经存在.小编就想,为什么当我们填写完use ...
- 12、Cocos2dx 3.0游戏开发找小三之3.0中的生命周期分析
重开发人员的劳动成果.转载的时候请务必注明出处:http://blog.csdn.net/haomengzhu/article/details/27706303 生命周期分析 在前面文章中我们执行了第 ...
- C/C++一些库函数的实现
1. 写出String的具体实现 类的定义: #include <iostream> #include <cstring> using namespace std; class ...
- 2015 Multi-University Training Contest 4 1001 Olympiad
代码: #include<cstdio> #include<cstring> #include<set> using namespace std; int vis[ ...
- HTML DOM Table 对象
Table 对象 Table 对象代表一个 HTML 表格. 在 HTML 文档中 <table> 标签每出现一次,一个 Table 对象就会被创建. Table 对象集合 集合 描述 c ...
- Node.js+Express搭建博客系统基本环境安装
1.下载安装node.js 官网下载地址:https://nodejs.org/en/download/ 2.安装express. 打开node命令行工具,在命令行中输入:npm install -g ...