0829学习 agile Posted on Aug 29 2023 面试 leetcode学习 - 除法求值-399 给你一个变量对数组 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 ] 注意:x 是未定义的 => -1.0 --- ```C# class Cell { public string Str; public double Value; public Cell(string str, double v) { Str = str; Value = v; } public override bool Equals(object? obj) { if (obj is Cell c) { return c.Str == Str; } return false; } } public double[] CalcEquation(IList<IList<string>> equations, double[] values, IList<IList<string>> queries) { var visited = new List<string>(); var info = new Dictionary<string, List<Cell>>(); for (int i = 0; i < equations.Count; i++) { var t = equations[i]; var key0 = t[0]; var key1 = t[1]; var v = values[i]; __Add(info, key0, key1, v); __Add(info, key1, key0, 1 / v); } foreach (var kv in info) { if (kv.Value.Count == 1) { var first = kv.Value.First(); } } var result = new double[queries.Count]; Array.Fill(result, -1); var cache = new HashSet<string>(); for (int i = 0; i < queries.Count; i++) { var t = queries[i]; var key0 = t[0]; var key1 = t[1]; cache.Clear(); __VisitedEquation(info, cache, key0, key1, result, i, 1); } return result; } private void __VisitedEquation(Dictionary<string, List<Cell>> info, HashSet<string> cache, string src, string target, double[] result, int index, double value) { if (cache.Contains(src)) { return; } cache.Add(src); if (src == target && info.ContainsKey(src)) { result[index] = value; return; } if (info.TryGetValue(src, out var cells)) { foreach (var cell in cells) { __VisitedEquation(info, cache, cell.Str, target, result, index, value * cell.Value); } } } private void __Add(Dictionary<string, List<Cell>> info, string key, string value, double v) { if (!info.TryGetValue(key, out var list)) { list = new List<Cell>(); info[key] = list; } var c = new Cell(value, v); if (!list.Contains(c)) { list.Add(c); } } public double[] CalcEquation2(IList<IList<string>> equations, double[] values, IList<IList<string>> queries) { var cache = new Dictionary<string, Dictionary<string, double>>(); for (int i = 0; i < equations.Count; i++) { var t = equations[i]; var v = values[i]; var key0 = t[0]; var key1 = t[1]; __Add(cache, key0, key1, v); __Add(cache, key1, key0, 1 / v); } var result = new Double[queries.Count]; for (int i = 0; i < queries.Count; i++) { var t = queries[i]; var key0 = t[0]; var key1 = t[1]; if (!cache.ContainsKey(key0) || !cache.ContainsKey(key1)) { result[i] = -1; } else { result[i] = __Dfs(cache, new Dictionary<string, HashSet<string>>(), key0, key1, 1); } } return result; } private double __Dfs(Dictionary<string, Dictionary<string, double>> cache, Dictionary<string, HashSet<string>> visited, string source, string target, double value) { if (source == target) { return value; } foreach (var kvPair in cache[source]) { var k = kvPair.Key; var v = kvPair.Value; if (!visited.TryGetValue(source, out var hashVisited)) { hashVisited = new HashSet<string>(); visited[source] = hashVisited; } double temp = -1; if (hashVisited.Add(k)) { temp = __Dfs(cache, visited, k, target, v); } if (Math.Abs(temp + 1) > 0.01f && k != target) { return temp * v; } if (k == target) { return temp; } } return -1; } private void __Add(Dictionary<string, Dictionary<string, double>> info, string key, string value, double v) { if (!info.TryGetValue(key, out var list)) { list = new Dictionary<string, double>(); info[key] = list; } if (!list.ContainsKey(value)) { list[value] = v; } } ``` --- 0824学习 unity3d-RectTransform