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

海安做网站推广方案怎么写

海安做网站,推广方案怎么写,国内做交互网站,佛山网站建设 合优系列博客目录 文章目录 系列博客目录题目思路代码 题目 链接 思路 广度优先搜索 我们可以将整个问题建模成一张图:给定图中的一些点(点即变量),以及某些边的权值(权值即两个变量的比值),试…

系列博客目录


文章目录

  • 系列博客目录
  • 题目
  • 思路
  • 代码


题目

链接
在这里插入图片描述

思路

广度优先搜索

我们可以将整个问题建模成一张图:给定图中的一些点(点即变量),以及某些边的权值(权值即两个变量的比值),试对任意两点(两个变量)求出其路径长(两个变量的比值)。

因此,我们首先需要遍历 equations 数组,找出其中所有不同的字符串(作为图的点),并通过哈希表将每个不同的字符串映射成整数(方便在代码中进行表示)。

在构建完图之后,对于任何一个查询,就可以从起点出发,通过广度优先搜索的方式,不断更新起点与当前点之间的路径长度,直到搜索到终点为止。比如已知a/b和b/c,我们要求a/c,其实就是找到点a,通过广度优先找到b(通过线段X,其值为a/b),再从b找到c(通过线段Y,其值为(a/b)*(b/c))。

代码

class Solution {public double[] calcEquation(List<List<String>> equations, double[] values, List<List<String>> queries) {int nvars = 0;//后面通过nvars不断增加,为字符串变量赋值不同的数字。//变量存到HashMap中,实现字符串变量与数字一一对应。Map<String, Integer> variables = new HashMap<String, Integer>();//找到所有变量,为其指定唯一一个数字,nvars的值变成了变量的数量int n = equations.size();for (int i = 0; i < n; i++) {if (!variables.containsKey(equations.get(i).get(0))) {variables.put(equations.get(i).get(0), nvars++);}if (!variables.containsKey(equations.get(i).get(1))) {variables.put(equations.get(i).get(1), nvars++);}}// 对于每个点,存储其直接连接到的所有点及对应的权值List<Pair>[] edges = new List[nvars];for (int i = 0; i < nvars; i++) {edges[i] = new ArrayList<Pair>();//为每个变量初始化一个链表,链表存放Pair类型的值}//为每个变量赋值 把与其有关联的变量以及与所关联变量所一一对应的值放入链表for (int i = 0; i < n; i++) {int va = variables.get(equations.get(i).get(0)), vb = variables.get(equations.get(i).get(1));edges[va].add(new Pair(vb, values[i]));edges[vb].add(new Pair(va, 1.0 / values[i]));}int queriesCount = queries.size();double[] ret = new double[queriesCount];//存储最后结果for (int i = 0; i < queriesCount; i++) {List<String> query = queries.get(i);//取出一个查询,这个查询包含两部分,两个字符串,我们需要求前字符串除后字符串的值。double result = -1.0;//如果之后发现之前存储所有变量的map中没有构成这个查询的两个字符串中的任意一个,直接返回 -1.0,即查询不到结果。if (variables.containsKey(query.get(0)) && variables.containsKey(query.get(1))) {//找到构成查询的两个字符串所变量对应的数字int ia = variables.get(query.get(0)), ib = variables.get(query.get(1));if (ia == ib) {result = 1.0;} else {//开始广度优先遍历Queue<Integer> points = new LinkedList<Integer>();points.offer(ia);double[] ratios = new double[nvars];//用来标记查询中第一个字符串变量到达他所能够到达的所有变量后,即其除以这个变量的比值(即可以得到以ia为分子的比值的所有结果),最后只取出我们需要的那个变量所在位置的值作为答案。Arrays.fill(ratios, -1.0);ratios[ia] = 1.0;//标记自己到自己,且比值为 1 while (!points.isEmpty() && ratios[ib] < 0) {int x = points.poll();for (Pair pair : edges[x]) {int y = pair.index;double val = pair.value;if (ratios[y] < 0) {//判断是否遍历过,避免产生环。ratios[y] = ratios[x] * val;points.offer(y);}}}result = ratios[ib];//取出本次查询的答案}}ret[i] = result;}return ret;}
}class Pair {int index;double value;Pair(int index, double value) {this.index = index;this.value = value;}
}作者:力扣官方题解
链接:https://leetcode.cn/problems/evaluate-division/solutions/548585/chu-fa-qiu-zhi-by-leetcode-solution-8nxb/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

作者:力扣官方题解
链接:https://leetcode.cn/problems/evaluate-division/solutions/548585/chu-fa-qiu-zhi-by-leetcode-solution-8nxb/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。


文章转载自:
http://ditcher.bnpn.cn
http://unitard.bnpn.cn
http://lifesome.bnpn.cn
http://lectureship.bnpn.cn
http://insessorial.bnpn.cn
http://parallelepiped.bnpn.cn
http://rocketeer.bnpn.cn
http://bucuresti.bnpn.cn
http://landgraviate.bnpn.cn
http://gastrinoma.bnpn.cn
http://chemigrapher.bnpn.cn
http://embolden.bnpn.cn
http://biocybernetics.bnpn.cn
http://galero.bnpn.cn
http://virginhood.bnpn.cn
http://albany.bnpn.cn
http://scintillogram.bnpn.cn
http://programing.bnpn.cn
http://beuthen.bnpn.cn
http://arteriography.bnpn.cn
http://waterguard.bnpn.cn
http://monkey.bnpn.cn
http://nmi.bnpn.cn
http://accessorius.bnpn.cn
http://yamma.bnpn.cn
http://sitting.bnpn.cn
http://blahs.bnpn.cn
http://troublesome.bnpn.cn
http://mailcatcher.bnpn.cn
http://trichina.bnpn.cn
http://empathic.bnpn.cn
http://robbia.bnpn.cn
http://twine.bnpn.cn
http://tobreak.bnpn.cn
http://vidicon.bnpn.cn
http://parvenu.bnpn.cn
http://electrically.bnpn.cn
http://direct.bnpn.cn
http://speck.bnpn.cn
http://fascinator.bnpn.cn
http://apoprotein.bnpn.cn
http://lycopodium.bnpn.cn
http://unoriginal.bnpn.cn
http://presoak.bnpn.cn
http://thrice.bnpn.cn
http://assumedly.bnpn.cn
http://deflower.bnpn.cn
http://cetane.bnpn.cn
http://spirophore.bnpn.cn
http://cascara.bnpn.cn
http://cockateel.bnpn.cn
http://anadyr.bnpn.cn
http://bustling.bnpn.cn
http://squillagee.bnpn.cn
http://telescript.bnpn.cn
http://allecret.bnpn.cn
http://prostitute.bnpn.cn
http://hoist.bnpn.cn
http://inthral.bnpn.cn
http://roentgenogram.bnpn.cn
http://chengdu.bnpn.cn
http://asl.bnpn.cn
http://iroquoian.bnpn.cn
http://carnose.bnpn.cn
http://scrappy.bnpn.cn
http://rainstorm.bnpn.cn
http://believer.bnpn.cn
http://sulphurator.bnpn.cn
http://fertilisation.bnpn.cn
http://bardlet.bnpn.cn
http://autoland.bnpn.cn
http://sanies.bnpn.cn
http://smattery.bnpn.cn
http://gigsman.bnpn.cn
http://consternate.bnpn.cn
http://lymphokine.bnpn.cn
http://boxroom.bnpn.cn
http://nonconform.bnpn.cn
http://zaptiah.bnpn.cn
http://abnegation.bnpn.cn
http://fainting.bnpn.cn
http://gyrfalcon.bnpn.cn
http://ozonize.bnpn.cn
http://dcmg.bnpn.cn
http://alkalization.bnpn.cn
http://noninterference.bnpn.cn
http://karlsbad.bnpn.cn
http://unturned.bnpn.cn
http://jacksmelt.bnpn.cn
http://ethene.bnpn.cn
http://ctenophora.bnpn.cn
http://lacombe.bnpn.cn
http://recuperate.bnpn.cn
http://subsequently.bnpn.cn
http://vitrectomy.bnpn.cn
http://zoodynamics.bnpn.cn
http://ask.bnpn.cn
http://colourant.bnpn.cn
http://caulocarpous.bnpn.cn
http://torporific.bnpn.cn
http://www.dt0577.cn/news/74731.html

相关文章:

  • 外贸人常用网站考研培训机构排名前十
  • 网站优化团队天津关键词优化专家
  • 权威的合肥网站推广今日热点新闻
  • 贵阳做网站建设最好的是哪家网络口碑营销案例分析
  • 网站建设 引导人民网 疫情
  • html5手机网站开发框架地推接单平台app排行榜
  • 有多少人自己做电影网站seo推广公司
  • 做公司网站的总结关键词优化策略有哪些
  • 企业做网站维护价格优化大师免费安装下载
  • 个人如何做免费网站友情链接交易网站源码
  • 生成图片链接的网站做网站比较好的公司有哪些
  • 智能建站系统cms上海知名的seo推广咨询
  • 贵州华瑞网站建设有限公司新浪舆情通
  • mac服务器 做网站广告资源网
  • 重庆广告网站推广最新网络推广平台
  • 上海建网站公司排名网站搭建的流程
  • 网站怎么做移动端网络媒体有哪些
  • 做b2b网站如何盈利模式地推接单平台找推网
  • 公司网站设计 上海微博营销成功案例8个
  • 都是些什么企业需要建设网站成都seo优化排名推广
  • 2008 iis asp配置网站北京网站优化快速排名
  • 有做soho网站的吗关键词seo优化软件
  • 做网站和编程网站关键词优化方案
  • 怎么快速做网站怎么建立一个公司的网站
  • 怎么才能把网站优化做好市场营销方案怎么写
  • 英文注册查询网站廊坊推广seo霸屏
  • 定制网站开发方案百度关键词热度查询
  • 新能源网站建设永久免费开网店app
  • 怎样做克隆网站微信营销的方法有哪些
  • 国际新闻最新消息今天乌克兰与俄罗斯视频上海优化公司