【leetcode】399. Evaluate Division

本文介绍了一种使用图算法解决给定变量方程组的方法。通过构建一个加权图,利用广度优先搜索算法来查找不同变量之间的关系及它们的比例值。适用于求解包含多个变量的比例方程组问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.

解题思路:建立一个图,图的权值记为:如题目给出a/b = 2.0 则对于边(a,b)权值为2.0;对于边(b,a)权值为1/2.0。在获得到问题字符串对后,对图进行搜索,并从一开始记录权值,每前进一个元素,将此地与上一个节点的权值乘以上一个节点保存的从初始点到上一个节点的权值的乘积。在图搜索到目标字符串后,返回数值并结束搜索。


代码如下:

#include <iostream>
#include <vector>
#include <list>
#include <string>
#include <queue>
using namespace std;
struct Node {
string mark;
double distence;
};
bool search(vector<string> visited, string mark) {
bool in = false;
for (int i = 0; i < visited.size(); i++) {
if (visited[i] == mark)
in = true;
}
return in;
}
double findDistance(vector<list<Node> >  graph, string from, string destination) {
queue<pair<string,double> > in;
vector<string> visited;
pair<string, double> temp;
temp.first = from;
temp.second = 1;
in.push(temp);
visited.push_back(from);
while (!in.empty()) {
// cout << "in" << endl;
pair<string, double> t = in.front();
// cout << "t:" << t.first<< " "<< t.second << endl;
in.pop();
bool have = false;
for (int i = 0; i < graph.size(); i++) {
if (graph[i].front().mark == t.first) {
if (from == destination) return 1;
have = true;
list<Node> ::iterator it = graph[i].begin();
it++;
for (it; it != graph[i].end(); it++) {
if (!search(visited, it->mark)) {
visited.push_back(it->mark);
// cout << it->mark << " :" << it->distence << " ";
pair<string, double> a;
a.first = it->mark;
a.second = it->distence * t.second;
// cout << a.second << endl;
in.push(a);
if (it->mark == destination) {
return a.second;
}
}
}

}
}
if (have == false) {
return -1;
}
}
return -1;
}
class Solution {
public:
vector<double> calcEquation(vector<pair<string, string> > equations, vector<double>& values, vector<pair<string, string> > queries) {
vector<list<Node> >  graph;
// cout << equations.size() << endl;
for (int i = 0; i < equations.size(); i++) {
// cout << "in" << endl;
string t1(equations[i].first);
string t2(equations[i].second);
bool had1 = false, had2 = false;
for (int j = 0; j < graph.size(); j++) {
if (graph[j].front().mark == t1) {
Node new1;
new1.mark = t2;
new1.distence = values[i];
graph[j].push_back(new1);
had1 = true;
}
if (graph[j].front().mark == t2) {
Node new1;
new1.mark = t1;
new1.distence = 1 / values[i];
graph[j].push_back(new1);
had2 = true;
}
}
if (had1 == false) {
list<Node> temp;
Node new1;
new1.mark = t1;
new1.distence = 1;
temp.push_back(new1);
graph.push_back(temp);
Node new2;
new2.mark = t2;
new2.distence = values[i];
graph[graph.size() - 1].push_back(new2);
}
if (had2 == false) {
list<Node> temp;
Node new1;
new1.mark = t2;
new1.distence = 1;
temp.push_back(new1);
graph.push_back(temp);
Node new2;
new2.mark = t1;
new2.distence = 1 / values[i];
graph[graph.size() - 1].push_back(new2);
}
}


/* for (int i = 0; i < graph.size(); i++) {
list<Node> ::iterator it = graph[i].begin();
cout << graph[i].front().mark << ": ";
it++;
for (int j = 1; j < graph[i].size(); j++) {
cout << it->mark << " -> " << it->distence << " ";
it++;
}
cout << endl;
}*/
vector<double> result;
for (int i = 0; i < queries.size(); i++) {
double b =  findDistance(graph, queries[i].first, queries[i].second);
result.push_back(b);
}
return result;
}
};

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值