当前位置:网站首页>leetcode-399: division evaluation
leetcode-399: division evaluation
2022-07-31 01:44:00 【chrysanthemum bat】
题目
给你一个变量对数组 equations 和一个实数值数组 values 作为已知条件,其中 equations[i] = [Ai, Bi] 和 values[i] 共同表示等式 Ai / Bi = values[i] .每个 Ai 或 Bi 是一个表示单个变量的字符串.
另有一些以数组 queries 表示的问题,其中 queries[j] = [Cj, Dj] 表示第 j 个问题,请你根据已知条件找出 Cj / Dj = ? 的结果作为答案.
返回 所有问题的答案 .如果存在某个无法确定的答案,则用 -1.0 替代这个答案.如果问题中出现了给定的已知条件中没有出现的字符串,也需要用 -1.0 替代这个答案.
注意:输入总是有效的.你可以假设除法运算中不会出现除数为 0 的情况,且不存在任何矛盾的结果.
示例 1:
输入:equations = [["a","b"],["b","c"]], values = [2.0,3.0], queries = [["a","c"],["b","a"],["a","e"],["a","a"],["x","x"]]
输出:[6.00000,0.50000,-1.00000,1.00000,-1.00000]
解释:
条件:a / b = 2.0, b / c = 3.0
问题:a / c = ?, b / a = ?, a / e = ?, a / a = ?, x / x = ?
结果:[6.0, 0.5, -1.0, 1.0, -1.0 ]
示例 2:
输入:equations = [["a","b"],["b","c"],["bc","cd"]], values = [1.5,2.5,5.0], queries = [["a","c"],["c","b"],["bc","cd"],["cd","bc"]]
输出:[3.75000,0.40000,5.00000,0.20000]
示例 3:
输入:equations = [["a","b"]], values = [0.5], queries = [["a","b"],["b","a"],["a","c"],["x","y"]]
输出:[0.50000,2.00000,-1.00000,-1.00000]
解题
方法一:并查集
Building a weighted directed graph+并查集
class UnionFind{
private:
vector<int> parent;
vector<double> weight;
public:
UnionFind(int n){
parent.resize(n);
weight.resize(n);
for(int i=0;i<n;i++){
parent[i]=i;
weight[i]=1;
}
}
int find(int index){
if(index==parent[index]) return index;
int origin=parent[index];
parent[index]=find(parent[index]);//At the same time, the parent node will be displayedweight也更新好
weight[index]*=weight[origin];
return parent[index];
}
void unite(int index1,int index2,double value){
int p1=find(index1);
int p2=find(index2);
if(p1!=p2){
parent[p1]=p2;
weight[p1]=weight[index2]*value/weight[index1];
}
}
double isConnected(int index1,int index2){
int p1=find(index1);
int p2=find(index2);
if(p1==p2){
return weight[index1]/weight[index2];
}
else return -1;
}
};
class Solution {
public:
vector<double> calcEquation(vector<vector<string>>& equations, vector<double>& values, vector<vector<string>>& queries) {
int equationSize=equations.size();
UnionFind uf(2*equationSize);
// 第 1 步:预处理,将变量的值与 id 进行映射,使得并查集的底层使用数组实现,方便编码
unordered_map<string,int> mp;
int id=0;
for(int i=0;i<equationSize;i++){
vector<string> equation=equations[i];
string s1=equation[0];
string s2=equation[1];
if(!mp.count(s1)){
mp[s1]=id++;
}
if(!mp.count(s2)){
mp[s2]=id++;
}
uf.unite(mp[s1],mp[s2],values[i]);
}
// 第 2 步:做查询
int queriesSize=queries.size();
vector<double> res(queriesSize);
for(int i=0;i<queriesSize;i++){
string s1=queries[i][0];
string s2=queries[i][1];
if(!mp.count(s1)||!mp.count(s2)) res[i]=-1;
else res[i]=uf.isConnected(mp[s1],mp[s2]);
}
return res;
}
};
边栏推荐
- 1.非类型模板参数 2.模板的特化 3.继承讲解
- 822. Walk the Grid
- Overview of prometheus monitoring
- 一个无经验的大学毕业生,可以转行做软件测试吗?我的真实案例
- case语句的综合结果,你究竟会了吗?【Verilog高级教程】
- [WeChat applet] This article takes you to understand data binding, event binding, event parameter transfer, and data synchronization
- Is there a way to earn 300 yuan a day by doing a side business?
- 九州云获评云计算标准化优秀成员单位
- link与@import的区别
- Bert usage and word prediction based on Keras_bert model
猜你喜欢
rpm install postgresql12
[WeChat applet] This article takes you to understand data binding, event binding, event parameter transfer, and data synchronization
"Real" emotions dictionary based on the text sentiment analysis and LDA theme analysis
MySQL (6)
JPEG Steganalysis of Digital Image Steganography
蓝牙mesh系统开发二 mesh节点开发
Word 表格跨页,仍然显示标题
Xiaohei's leetcode journey: 104. The maximum depth of a binary tree
pycharm重命名后无法运行(报错: can‘t open file......No such file or directory)
Set the browser scrollbar style
随机推荐
数字图像隐写术之卡方分布
RTL8720DN开发笔记一 环境搭建与mqtt实例
What have I experienced when I won the offer of BAT and TMD technical experts?
[WeChat applet] This article takes you to understand data binding, event binding, event parameter transfer, and data synchronization
leetcode-128:最长连续序列
1.非类型模板参数 2.模板的特化 3.继承讲解
蓝牙mesh系统开发二 mesh节点开发
Parameter introduction and selection points of wireless module
【微信小程序】一文带你了解数据绑定、事件绑定以及事件传参、数据同步
keep-alive缓存组件
进程间通信学习笔记
Meta元宇宙部门第二季度亏损28亿 仍要继续押注?元宇宙发展尚未看到出路
九州云入选“可信云最新评估体系及2022年通过评估企业名单”
case语句的综合结果,你究竟会了吗?【Verilog高级教程】
Multiplication, DFS order
16、注册中心-consul
.NET 跨平台应用开发动手教程 |用 Uno Platform 构建一个 Kanban-style Todo App
leetcode-952:按公因数计算最大组件大小
What does a software test report contain?
观察者(observer)模式(一)