这是leetcode的第399题--Redund" />
[一起来刷leetcode吧][11] 您所在的位置:网站首页 秋梨膏是凉性还是热性食物呢 [一起来刷leetcode吧][11]

[一起来刷leetcode吧][11]

#[一起来刷leetcode吧][11]| 来源: 网络整理| 查看: 265

这篇文章是程序自动发表的,详情可以见 这里 href="http://ounix1xcw.bkt.clouddn.com/github.markdown.css" rel="stylesheet">

这是leetcode的第399题--Redundant Connection

  题目 Equations are given in the format A / B = k, where A and B are variables represented as strings, and k is a real number (floating point number). Given some queries, return the answers. If the answer does not exist, return -1.0.

Example: Given a / b = 2.0, b / c = 3.0. queries are:a / c = ?, b / a = ?, a / e = ?, a / a = ?, x / x = ? . return [6.0, 0.5, -1.0, 1.0, -1.0 ].

The input is: vector > equations, vector & values, vector > queries , where equations.size() == values.size(), and the values are positive. This represents the equations. Return vector .

According to the example above:

equations = [ ["a", "b"], ["b", "c"] ], values = [2.0, 3.0], queries = [ ["a", "c"], ["b", "a"], ["a", "e"], ["a", "a"], ["x", "x"] ].

The input is always valid. You may assume that evaluating the queries will result in no division by zero and there is no contradiction.   思路 这道题由于是除法,会想到有向图,但是发现,a->b, c->b有可以推出a,b关系,所以连通即可。为了能方便搜索,我又增加了一条反向边vuy(代码第161行),当然权值为倒数,可是这样增加了深搜难度,于是要解决环的搜索问题(代码第127行)   

show me the code from collections import Iterable,deque class vertex: def __init__(self,mark,val=None ,firstEdge = None): self.mark = mark self.val = val self.firstEdge = firstEdge self.isVisited = False def __str__(self): if '0'


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有