leetcode 399. 除法求值(bfs)

给你一个变量对数组 equations 和一个实数值数组 values 作为已知条件,其中 equations[i] = [Ai, Bi] 和 values[i] 共同表示等式 Ai / Bi = values[i] 。每个 Ai 或 Bi 是一个表示单个变量的字符串。

另有一些以数组 queries 表示的问题,其中 queries[j] = [Cj, Dj] 表示第 j 个问题,请你根据已知条件找出 Cj / Dj = ? 的结果作为答案。

返回 所有问题的答案 。如果存在某个无法确定的答案,则用 -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 ]

代码

class Solution {class Pair{int index;double val;public Pair(int index, double val) {this.index = index;this.val = val;}}public double[] calcEquation(List<List<String>> equations, double[] values, List<List<String>> queries) {Map<String,Integer> map=new HashMap<>();int cur=0;for(int i=0;i<equations.size();i++)//记录字符串和其编号{if(!map.containsKey(equations.get(i).get(0)))map.put(equations.get(i).get(0),cur++);if(!map.containsKey(equations.get(i).get(1)))map.put(equations.get(i).get(1),cur++);}List<Pair>[] lists=new List[cur];for(int i=0;i<lists.length;i++){lists[i]=new ArrayList<Pair>();}for(int i=0;i<equations.size();i++)//记录各个节点之间的权重{int va=map.get(equations.get(i).get(0)),vb=map.get(equations.get(i).get(1));lists[va].add(new Pair(vb,values[i]));lists[vb].add(new Pair(va,1/values[i]));}double[] ret=new double[queries.size()];for(int i=0;i<queries.size();i++){double res=-1;if(map.containsKey(queries.get(i).get(0))&&map.containsKey(queries.get(i).get(1)))//出现不存在的符号返回-1{int ia=map.get(queries.get(i).get(0)),ib=map.get(queries.get(i).get(1));if(ia==ib){res=1;}else{//bfs,找出去目标节点的路径Queue<Integer> queue=new LinkedList<>();queue.add(ia);double[] ra=new double[cur];Arrays.fill(ra,-1);//-1代表没到达的节点ra[ia]=1;while (!queue.isEmpty()&&ra[ib]<0){int now=queue.poll();for(Pair pair:lists[now]){int idx=pair.index;double val=pair.val;if(ra[idx]<0){ra[idx]=val*ra[now];queue.offer(idx);}}}res=ra[ib];}}ret[i]=res;}return ret;}
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/392362.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【0718作业】收集和整理面向对象的六大设计原则

面向对象的六大设计原则 &#xff08;1&#xff09;单一职责原则——SRP &#xff08;2&#xff09;开闭原则——OCP &#xff08;3&#xff09;里式替换原则——LSP &#xff08;4&#xff09;依赖倒置原则——DIP &#xff08;5&#xff09;接口隔离原则——ISP &#xff08;…

数据科学 python_适用于数据科学的Python vs(和)R

数据科学 pythonChoosing the right programming language when taking on a new project is perhaps one of the most daunting decisions programmers often make.在进行新项目时选择正确的编程语言可能是程序员经常做出的最艰巨的决定之一。 Python and R are no doubt amon…

如何进行有效的需求调研

一、什么是需求调研&#xff1f;需求调研对于一个应用软件开发来说&#xff0c;是一个系统开发的开始阶段&#xff0c;它的输出“软件需求分析报告”是设计阶段的输入&#xff0c;需求调研的质量对于一个应用软件来说&#xff0c;是一个极其重要的阶段&#xff0c;它的质量在一…

java中直角三角形第三条边,Java编程,根据输入三角形的三个边边长,程序能判断三角形类型为:等边、等腰、斜角、直角三角形,求代码...

private static Scanner sc;private static int edge[] new int[3];public static void main(String[] args) {System.out.println("请输入三角形的三条边");sc new Scanner(System.in);input();}public static void input() {int index 0;//数组下标while (sc.ha…

react中使用构建缓存_使用React和Netlify从头开始构建电子商务网站

react中使用构建缓存In this step-by-step, 6-hour tutorial from Coding Addict, you will learn to build an e-commerce site from scratch using React and create-react-app.在这个Coding Addict的分步&#xff0c;为时6小时的教程中&#xff0c;您将学习使用React和creat…

Django+Vue前后端分离项目的部署

部署静态文件&#xff1a; 静态文件有两种方式 1&#xff1a;通过django路由访问 2&#xff1a;通过nginx直接访问 方式1&#xff1a; 需要在根目录的URL文件中增加 url(r^$, TemplateView.as_view(template_name"index.html")),作为入口&#xff0c;在setting中更改…

leetcode 547. 省份数量(bfs)

有 n 个城市&#xff0c;其中一些彼此相连&#xff0c;另一些没有相连。如果城市 a 与城市 b 直接相连&#xff0c;且城市 b 与城市 c 直接相连&#xff0c;那么城市 a 与城市 c 间接相连。 省份 是一组直接或间接相连的城市&#xff0c;组内不含其他没有相连的城市。 给你一…

r怎么对两组数据统计检验_数据科学中最常用的统计检验是什么

r怎么对两组数据统计检验Business analytics and data science is a convergence of many fields of expertise. Professionals form multiple domains and educational backgrounds are joining the analytics industry in the pursuit of becoming data scientists.业务分析和…

win10专业版激活(cmd方式)

转载于:https://www.cnblogs.com/bug-baba/p/11225322.html

mit景观生成技术_永远不会再为工作感到不知所措:如何使用MIT技术

mit景观生成技术by Sihui Huang黄思慧 永远不会再为工作感到不知所措&#xff1a;如何使用MIT技术 (Never feel overwhelmed at work again: how to use the M.I.T. technique) Have you ever felt exhausted after a day at work? At the end of a busy day, you couldn’t …

leetcode 189. 旋转数组

给定一个数组&#xff0c;将数组中的元素向右移动 k 个位置&#xff0c;其中 k 是非负数。 示例 1: 输入: [1,2,3,4,5,6,7] 和 k 3 输出: [5,6,7,1,2,3,4] 解释: 向右旋转 1 步: [7,1,2,3,4,5,6] 向右旋转 2 步: [6,7,1,2,3,4,5] 向右旋转 3 步: [5,6,7,1,2,3,4] 代码 cla…

aws ec2 php,如何使用php aws sdk启动和停止ec2实例

以下是从定义的AMI启动计算机的基本示例&#xff1a;$image_id ami-3d4ff254; //Ubuntu 12.04$min 1; //the minimum number of instances to start$max 1; //the maximum number of instances to start$options array(SecurityGroupId > default, //replace with your …

python3 递归

递归调用&#xff1a;  在调用一个函数的过程中&#xff0c;直接或者简介调用了该函数本身 必须有一个明确的结束条件 递归特性:  1. 必须有一个明确的结束条件  2. 每次进入更深一层递归时&#xff0c;问题规模相比上次递归都应有所减少  3. 递归效率不高&#xff0c;…

深度学习概述_深度感测框架概述

深度学习概述I have found the DeepSense framework as one of the promising deep learning architectures for processing Time-Series sensing data. In this brief and intuitive overview, I’ll present the main ideas of the original paper titled “Deep Sense: A Un…

css响应式网格布局生成器_如何使用网格布局模块使用纯CSS创建响应表

css响应式网格布局生成器TL; DR (TL;DR) The most popular way to display a collection of similar data is to use tables, but HTML tables have the drawback of being difficult to make responsive.显示相似数据集合的最流行方法是使用表&#xff0c;但是HTML表具有难以响…

Axure注册码

适用版本 Axure 8.1.0.3377 zdfans.com gP5uuK2gHiIVO3YFZwoKyxAdHpXRGNnZWN8Obntqv7FF3pAz7dTu8B61ySxli 转载于:https://www.cnblogs.com/mengjianzhou/p/11226260.html

命令行窗口常用的一些小技巧

一. 打开命令行窗口的方式 1. 按住【shift】键&#xff0c;在桌面右击&#xff0c;选择“在此处打开命令行窗口(W)”,如下图所示&#xff1a; 2. 按住【开始】 R快捷键&#xff0c;弹出运行窗口&#xff0c;输入cmd&#xff0c;回车&#xff08;确定&#xff09;即可。 二. 常用…

php soapserver 参数,PHP SoapServer – 节点中的属性

PHP肥皂功能是如此疯狂,我从来没有发现它的错误.我试图通过SOAP API连接和更新数据到zimbra,并且有很多问题.所以我使用了SimpleXMLElement&卷曲:)在那里你可以像这样构建你的XML&#xff1a;$xml new SimpleXMLElement(); // create your base$xml $xml->addChild(ta…

leetcode 123. 买卖股票的最佳时机 III(dp)

给定一个数组&#xff0c;它的第 i 个元素是一支给定的股票在第 i 天的价格。 设计一个算法来计算你所能获取的最大利润。你最多可以完成 两笔 交易。 注意&#xff1a;你不能同时参与多笔交易&#xff08;你必须在再次购买前出售掉之前的股票&#xff09;。 示例 1: 输入&…

为什么即使在班级均衡的情况下,准确度仍然令人困扰

Accuracy is a go-to metric because it’s highly interpretable and low-cost to evaluate. For this reason, accuracy — perhaps the most simple of machine learning metrics — is (rightfully) commonplace. However, it’s also true that many people are too comfo…