当前位置: 首页 > news >正文

399. 除法求值

https://leetcode.cn/problems/evaluate-division/description/?envType=study-plan-v2&envId=top-interview-150

思路:读完题后我们可以发现这题的考察已经很明确了就是考我们矩阵,我们将矩阵构建出来后,这题就变成可达性分析题了。
所以解题步骤就是:1.构建矩阵 2.递归判断是否可达

class Solution {public double[] calcEquation(List<List<String>> equations, double[] values, List<List<String>> queries) {// 处理所有出现的运算数int cnt = 0;HashMap<String, Integer> ch = new HashMap<>();for(int i = 0; i < equations.size(); i++) {String num1 = equations.get(i).get(0);String num2 = equations.get(i).get(1);if(!ch.containsKey(num1)) {ch.put(num1, cnt++);}if(!ch.containsKey(num2)) {ch.put(num2, cnt++);}}// 填充矩阵double[][] matrix = new double[cnt][cnt];for(int i = 0; i < equations.size(); i++) {String num1 = equations.get(i).get(0);String num2 = equations.get(i).get(1);matrix[ch.get(num1)][ch.get(num2)] = values[i];matrix[ch.get(num2)][ch.get(num1)] = 1.0 / values[i];}double[] res = new double[queries.size()];for(int i = 0; i < queries.size(); i++) {String num1 = queries.get(i).get(0);String num2 = queries.get(i).get(1);if(!ch.containsKey(num1) || !ch.containsKey(num2)) { // 如果出现没有出现的运算数,则直接返回-1res[i] = -1.0;} else { // 递归寻找可达路径// 标记某点是否到达过boolean[] signed = new boolean[cnt];signed[ch.get(num1)] = true;double sum = 1;res[i] = dfs(matrix, ch.get(num1), ch.get(num2), signed);}}return res;}public double dfs(double[][] matrix, int i, int j, boolean[] signed) {if(matrix[i][j] != 0) { // 如果可达,则直接返回结果return matrix[i][j];}double sum = 1;for(int k = 0; k < matrix.length; k++) {if(matrix[i][k] != 0 && !signed[k]) { // 如果可达且未到达过signed[k] = true;double dd = dfs(matrix, k, j, signed); // 递归寻找可达路径if(dd != -1) { // 如果可达就乘上权重sum *= matrix[i][k] * dd;return sum;}}}// 遍历所有情况没有找到可达路径就返回-1return -1;}public static void main(String[] args) {List<List<String>> equations = new ArrayList<>();equations.add(new ArrayList<>(List.of("x1","x2")));equations.add(new ArrayList<>(List.of("x2","x3")));equations.add(new ArrayList<>(List.of("x3","x4")));equations.add(new ArrayList<>(List.of("x4","x5")));double[] values = {3.0,4.0,5.0,6.0};List<List<String>> queries = new ArrayList<>();queries.add(new ArrayList<>(List.of("x1","x5")));queries.add(new ArrayList<>(List.of("x5","x2")));queries.add(new ArrayList<>(List.of("x2","x4")));queries.add(new ArrayList<>(List.of("x2","x2")));queries.add(new ArrayList<>(List.of("x2","x9")));queries.add(new ArrayList<>(List.of("x9","x9")));System.out.println(Arrays.toString(new Solution().calcEquation(equations, values, queries)));}
}

http://www.dtcms.com/a/191945.html

相关文章:

  • 遗传算法求解旅行商问题分析
  • 【FMC216】基于 VITA57.1 的 2 路 TLK2711 发送、2 路 TLK2711 接收 FMC 子卡模块
  • [学习]RTKLib详解:tle.c(系列终章)
  • Android 图片自动拉伸不变形,点九
  • windows ffmpeg msvc x64编译
  • pytorch训练可视化工具---TensorBoard
  • 【web应用】配置Java JDK与maven3的环境变量
  • Docker实现MySQL数据库主从复制
  • 《棒球百科》市运会是什么级别的比赛·棒球1号位
  • nt!MiAllocateWsle函数分析之设置Wsle[WorkingSetIndex]
  • Golang
  • 基于策略的强化学习方法之近端策略优化(PPO)深度解析
  • 2025.05.14华为机考笔试题-第一题-100分
  • xp_cmdshell bcp 导出文件
  • 测试--BUG(软件测试⽣命周期 bug的⽣命周期 与开发产⽣争执怎么办)
  • 牛客网NC22157:牛牛学数列2
  • 编程题 03-树3 Tree Traversals Again【PAT】
  • AI实时对话的通信基础,WebRTC技术综合指南
  • GPU与NPU异构计算任务划分算法研究:基于强化学习的Transformer负载均衡实践
  • Go 语言中接口类型转换为具体类型
  • Automatic Recovery of the Atmospheric Light in Hazy Images论文阅读
  • RabbitMQ 消息模式实战:从简单队列到复杂路由(一)
  • 用FileCodeBox打造私有文件传输:Ubuntu环境保姆级部署教程!
  • python算法-最大连续1的个数删除有序数组中的重复项--Day020
  • go依赖查询工具之godepgraph(分析main.go的依赖树)
  • 2025.05.14华为机考笔试题-第二题-200分
  • 鸿蒙OSUniApp制作多选框与单选框组件#三方框架 #Uniapp
  • ET EntityRef EntityWeakRef 类分析
  • C#语法基础
  • 分布式调度的--多模型协同工作实践方案