• LeetCode220912_102、除法求值


    给你一个变量对数组 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 ]

    来源:力扣(LeetCode)
    链接:https://leetcode.cn/problems/evaluate-division
    著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

    题解、并查集,加权值传递在合并和查询过程中。

    1. class Solution{
    2. public double[] calcEquation(List<List<String>> equations, double[] values, List> queries){
    3. int equationsSize = equations.size();
    4. UnionFind uf = new UnionFind(2 * equationsSize);
    5. Map<String, Integer> map = new HashMap<>(2 * equationsSize);
    6. int id = 0;
    7. for(int i = 0; i < equationsSize; i++){
    8. List<String> equation = equations.get(i);
    9. String var1 = equation.get(0);
    10. String var2 = equation.get(1);
    11. if(!map.containsKey(var1)){
    12. map.put(var1, id);
    13. id++;
    14. }
    15. if(!map.containsKey(var2)){
    16. map.put(var2, id);
    17. id++;
    18. }
    19. uf.union(map.get(var1), map.get(var2), values[i]);
    20. }
    21. int queriesSize = queries.size();
    22. double[] res = new double[queriesSize];
    23. for(int i = 0; i < queriesSize; i++){
    24. String var1 = queries.get(i).get(0);
    25. String var2 = queries.get(i).get(1);
    26. Integer id1 = map.get(var1);
    27. Integer id2 = map.get(var2);
    28. if(id1 == null || id2 == null){
    29. res[i] = -1.0d;
    30. }else{
    31. res[i] = uf.isConnected(id1, id2);
    32. }
    33. }
    34. return res;
    35. }
    36. private class UnionFind{
    37. private int[] parent;
    38. private double[] weight;
    39. public UnionFind(int x){
    40. parent = new int[x];
    41. weight = new double[x];
    42. for(int i = 0; i < x; i++){
    43. parent[i] = i;
    44. weight[i] = 1.0d;
    45. }
    46. }
    47. public void union(int x, int y, double value){
    48. int rootX = find(x);
    49. int rootY = find(y);
    50. if(rootX == rootY) return;
    51. parent[rootX] = rootY;
    52. weight[rootX] = weight[y] * value / weight[x];
    53. }
    54. public int find(int x){
    55. if(x != parent[x]){
    56. int origin = parent[x];
    57. parent[x] = find(parent[x]);
    58. weight[x] *= weight[origin];
    59. }
    60. return parent[x];
    61. }
    62. public double isConnected(int x, int y){
    63. int rootX = find(x);
    64. int rootY = find(y);
    65. if(rootX == rootY) return weight[x] / weight[y];
    66. else return -1.0d;
    67. }
    68. }
    69. }

  • 相关阅读:
    jq工具及其常用用法
    Dubbo服务控制台Dubbo Admin配置
    Ubuntu的中文乱码问题
    【文献导读】XPBD: Position-Based Simulation of Compliant Constrained Dynamics
    Java.lang.Class类 isInterface()方法有什么功能呢?
    Vue3:响应式数据的基本使用(ref、reactive)
    空间滤波-统计排序滤波器
    【iOS第三周总结】- UI学生管理系统
    Springboot+高校考勤小程序 毕业设计-附源码131039
    echarts图表区间展示
  • 原文地址:https://blog.csdn.net/Zoro_666/article/details/126823312