[leetcode] 399. Evaluate Division
看到这道题,2个点和一个权值,然后想到图,但是leetcode就是这样,没给数据范围,感觉写起来很费劲,然后就开始用图来做,添加边的时候,注意正向边和反向变,然后查询的时候,先判断2个点是否都出现,然后判断是不是自己到自己,最后用dfs或者bfs到图里面去搜索,第一次交的时候,忘记用vis标记访问过的点,导致tle,加上之后,就ac了,套路一般吧,不知道还有什么trick。
double dfs(vector<vector<pair<int, double> > > &e, int x, int y) {
int n = e.size();
vector<double> dis(n, 1);
queue<int> q;
q.push(x);
vector<bool> tag(n, 0);
while(!q.empty()) {
int u = q.front(); q.pop();
//cout << u << endl;
for (int i = 0; i < e[u].size(); i++) {
int a = e[u][i].first;
double b = e[u][i].second;
if(tag[a]) continue;
dis[a] = dis[u] * b;
if(a == y) return dis[a];
q.push(a);
tag[a] = 1;
}
}
return -1.0;
}
vector<double> calcEquation(vector<pair<string, string>> equations, vector<double>& values, vector<pair<string, string>> query) {
vector<vector<pair<int, double> > > e;
map<string, int> m;
for (int i = 0; i < equations.size(); i++) {
string x = equations[i].first, y = equations[i].second;
if(!m.count(x)) {
vector<pair<int, double> > t;
m[x] = e.size(); e.push_back(t);
}
if(!m.count(y)) {
vector<pair<int, double> > t;
m[y] = e.size(); e.push_back(t);
}
int a = m[x],b = m[y];
e[a].push_back({b, values[i]});
e[b].push_back({a, 1.0 / values[i]});
}
//cout << "ad" << endl;
//cout << e.size() << endl;
vector<double> res;
for (int i = 0; i < query.size(); i++) {
string x = query[i].first, y = query[i].second;
//cout <<x << " " << y << endl;
if(!m.count(x) || !m.count(y)) {
res.push_back(-1.0);
} else {
if(x == y) {
res.push_back(1.0);
} else {
int a = m[x], b = m[y];
double t = dfs(e, a, b);
res.push_back(t);
}
}
}
return res;
}
[leetcode] 399. Evaluate Division的更多相关文章
- LN : leetcode 399 Evaluate Division
lc 399 Evaluate Division 399 Evaluate Division Equations are given in the format A / B = k, where A ...
- [LeetCode] 399. Evaluate Division 求除法表达式的值
Equations are given in the format A / B = k, where A and B are variables represented as strings, and ...
- [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 +, -, ...
随机推荐
- 关于Windows文件名和路径名的那些事
博客搬到了fresky.github.io - Dawei XU,请各位看官挪步.最新的一篇是:关于Windows文件名和路径名的那些事.
- Python FTP多线程爆破脚本
初学python, 自己编写了个FTP多线爆破小脚本代码很丑= = #!usr/bin/env python #!coding=utf-8 __author__='zhengjim' from ftp ...
- smarty3--registerPlugin()函数报错问题
smarty版本:smarty3.1.30 registerPlugin错误信息: Notice: Trying to get property of non-object in E:\Joomla\ ...
- IOS 横屏中添加UIImagePickerController获取系统图片
今天写ipad的项目,然后需要调用系统相册选择图片,然后用了UIImagePickerController ,崩溃了,后来查了一下,UIImagePickerController只支持竖屏,但是... ...
- IOS7 隐藏状态栏
- (UIStatusBarStyle)preferredStatusBarStyle { return UIStatusBarStyleLightContent; } // - (BOOL)pref ...
- 在WWDC 2014上,没提到的iOS 8 八大新特性
今天凌晨1点,36氪如约为大家研磨出WWDC 2014全程 "贴身直播"(我不得不佩服牺牲个人时间,熬夜为大家奉上好文的5位氪星人:JasonZheng.WANGJINGYU.pa ...
- 【转】memcached工作原理介绍
FROM: http://my.oschina.net/flynewton/blog/8984 官方主页: http://memcached.org/ 面临的问题 对于高并发高访问的Web应用程序来 ...
- 【转】bt协议的详细分析
转自:https://baoz.net/bt-protocol/ 一 BT系统的组成结构 1 普通的Web服务器: 例如Apache或IIS服务器 2 一个静态的种子文件: 即. ...
- 【分享】4412开发板-嵌入式Linux开发须要掌握的基础知识和技能
本文转自迅为电子论坛:http://www.topeetboard.com 1.Linux 基础 安装Linux操作系统 Linux文件系统 Linux经常使用命令 Linux启动过程具体解释 熟悉L ...
- java中public等权限问题和final的使用
1.public:public表明该数据成员.成员函数是对所有用户开放的,所有用户都可以直接进行调用 2.private:private表示私有,私有的意思就是除了class自己之外,任何人都不可以直 ...