Blage's Coding Blage's Coding
Home
算法
  • 手写Spring
  • SSM
  • SpringBoot
  • JavaWeb
  • JAVA基础
  • 容器
  • Netty

    • IO模型
    • Netty初级
    • Netty原理
  • JVM
  • JUC
  • Redis基础
  • 源码分析
  • 实战应用
  • 单机缓存
  • MySQL

    • 基础部分
    • 实战与处理方案
    • 面试
  • ORM框架

    • Mybatis
    • Mybatis_Plus
  • SpringCloudAlibaba
  • MQ消息队列
  • Nginx
  • Elasticsearch
  • Gateway
  • Xxl-job
  • Feign
  • Eureka
  • 面试
  • 工具
  • 项目
  • 关于
🌏本站
🧸GitHub (opens new window)
Home
算法
  • 手写Spring
  • SSM
  • SpringBoot
  • JavaWeb
  • JAVA基础
  • 容器
  • Netty

    • IO模型
    • Netty初级
    • Netty原理
  • JVM
  • JUC
  • Redis基础
  • 源码分析
  • 实战应用
  • 单机缓存
  • MySQL

    • 基础部分
    • 实战与处理方案
    • 面试
  • ORM框架

    • Mybatis
    • Mybatis_Plus
  • SpringCloudAlibaba
  • MQ消息队列
  • Nginx
  • Elasticsearch
  • Gateway
  • Xxl-job
  • Feign
  • Eureka
  • 面试
  • 工具
  • 项目
  • 关于
🌏本站
🧸GitHub (opens new window)
  • 数组

  • 链表

  • 字符串

  • 二叉树

  • 动态规划

  • 深搜回溯

    • 46.全排列
    • 93.复原IP地址
    • 1079.活字印刷
    • 6441. 求一个整数的惩罚数
    • 47. 全排列 II
    • 78. 子集
    • 79. 单词搜索
    • 207. 课程表
    • 399. 除法求值
      • 1.dfs+回溯
    • 22. 括号生成
    • 39. 组合总和
    • 2746. 字符串连接删减字母
    • 931. 下降路径最小和
    • 40. 组合总和 II
    • 332. 重新安排行程
    • 51. N 皇后
    • 37. 解数独
    • 2050. 并行课程 III
    • 841. 钥匙和房间
    • 2850. 将石头分散到网格图的最少移动次数
    • 2316. 统计无向图中无法互相到达点对数
    • 剑指offer12
    • 剑指offer38
  • 数学贪心

  • 堆栈队列

  • 前缀和

  • 算法设计

  • 位运算

  • WA

  • 算法
  • 深搜回溯
phan
2023-06-11
目录

399. 除法求值

# 399. 除法求值 (opens new window)

# 1.dfs+回溯

分析:深搜消除匹配。注意匹配过程中可能会出现循环依赖的情况,因此需要使用标志位列表,标记使用过的字符串,防止搜索时无限循环。退出当前搜索之后需要对使用位进行回溯。

搜索时当分子分母相同时,可能存在如下情况:

  • 经过多次匹配消元得到的最终结果
  • 搜索前可以直接消元,属于在等式中出现过的字符串,此时结果为1
  • 搜索前可以直接消元,属于在等式中未出现过的字符串,置为-1

剪枝:如果某个分支已经成功消元,那么需要一个判断位success回传给父节点,直接退出搜索。否则得到的结果,会在“判断无法消元”的分支中重写为-1覆盖。

class Solution {
    double[] res;
    boolean success;
    public double[] calcEquation(List<List<String>> equations, double[] values, List<List<String>> queries) {
        List<String> used = new ArrayList<>();
        res = new double[queries.size()];
        for (int i = 0; i < queries.size(); i++) {
            success = false;
            List<String> list = queries.get(i);
            dfs(equations, values, used, list.get(0), list.get(1), 1, i);
        }
        return res;
    }

    public void dfs(List<List<String>> equations, double[] values, List<String> used, String source, String des, double mul, int index) {
        if (source.equals(des)) {
            if (used.size() > 0)
                res[index] = mul;
            else {
                boolean check = false;
                for (int i = 0; i < equations.size(); i++) {
                    List<String> list = equations.get(i);
                    if (list.get(0).equals(source) || list.get(1).equals(source)) {
                        check = true;
                    }
                }
                if (!check) res[index] = (double) -1.0;
                else res[index] = (double) 1.0;
            }
            success = true;
            return;
        }
        boolean check = false;
        for (int i = 0; i < equations.size(); i++) {
            if (success) return;
            List<String> list = equations.get(i);
            if (list.get(0).equals(source) && !used.contains(list.get(1))) {
                check = true;
                used.add(list.get(0));
                dfs(equations, values, used, list.get(1), des, mul * values[i], index);
                used.remove(used.size() - 1);
                if (list.get(1).equals(des)) break;

            }
            if (list.get(1).equals(source) && !used.contains(list.get(0))) {

                check = true;
                used.add(list.get(1));
                dfs(equations, values, used, list.get(0), des, mul / values[i], index);
                used.remove(used.size() - 1);
                if (list.get(0).equals(des)) break;
            }
        }
        if (!check) res[index] = (double) -1.0;
    }
}
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
编辑 (opens new window)
#Leetcode#回溯
上次更新: 2023/12/15, 15:49:57
207. 课程表
22. 括号生成

← 207. 课程表 22. 括号生成→

Theme by Vdoing | Copyright © 2023-2024 blageCoder
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式