399\. 除法求值
# 399. 除法求值 (opens new window)
# Description
Difficulty: 中等
Related Topics: 深度优先搜索 (opens new window), 广度优先搜索 (opens new window), 并查集 (opens new window), 图 (opens new window), 数组 (opens new window), 最短路 (opens new window)
给你一个变量对数组 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 ]
1
2
3
4
5
6
2
3
4
5
6
示例 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]
1
2
2
示例 3:
输入:equations = [["a","b"]], values = [0.5], queries = [["a","b"],["b","a"],["a","c"],["x","y"]]
输出:[0.50000,2.00000,-1.00000,-1.00000]
1
2
2
提示:
1 <= equations.length <= 20
equations[i].length == 2
- 1 <= Ai.length, Bi.length <= 5
values.length == equations.length
0.0 < values[i] <= 20.0
1 <= queries.length <= 20
queries[i].length == 2
- 1 <= Cj.length, Dj.length <= 5
- Ai, Bi, Cj, Dj 由小写英文字母与数字组成
# Solution
Language: JavaScript
用相邻矩阵把图的关系记录下来,然后把能推断出来的点都推断出来。
如果需要很多返回的解,比较有优势。
/**
* @param {string[][]} equations
* @param {number[]} values
* @param {string[][]} queries
* @return {number[]}
*/
// 方法一:广度优先搜索
var calcEquation = function(equations, values, queries) {
let nvars = 0;
const variables = new Map();
const n = equations.length;
for (let i = 0; i < n; i++) {
if (!variables.has(equations[i][0])) {
variables.set(equations[i][0], nvars++);
}
if (!variables.has(equations[i][1])) {
variables.set(equations[i][1], nvars++);
}
}
// 对于每个点,存储其直接连接到的所有点及对应的权值
const edges = new Array(nvars).fill(0);
for (let i = 0; i < nvars; i++) {
edges[i] = [];
}
for (let i = 0; i < n; i++) {
const va = variables.get(equations[i][0]), vb = variables.get(equations[i][1]);
edges[va].push([vb, values[i]]);
edges[vb].push([va, 1.0 / values[i]]);
}
const queriesCount = queries.length;
const ret = [];
for (let i = 0; i < queriesCount; i++) {
const query = queries[i];
let result = -1.0;
if (variables.has(query[0]) && variables.has(query[1])) {
const ia = variables.get(query[0]), ib = variables.get(query[1]);
if (ia === ib) {
result = 1.0;
} else {
const points = [];
points.push(ia);
const ratios = new Array(nvars).fill(-1.0);
ratios[ia] = 1.0;
while (points.length && ratios[ib] < 0) {
const x = points.pop();
for (const [y, val] of edges[x]) {
if (ratios[y] < 0) {
ratios[y] = ratios[x] * val;
points.push(y);
}
}
}
result = ratios[ib];
}
}
ret[i] = result;
}
return ret;
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64