這篇文章主要講解了“l(fā)eetcode除法求值的方法是什么”,文中的講解內(nèi)容簡(jiǎn)單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來(lái)研究和學(xué)習(xí)“l(fā)eetcode除法求值的方法是什么”吧!
創(chuàng)新互聯(lián)建站專(zhuān)注為客戶(hù)提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于網(wǎng)站設(shè)計(jì)制作、網(wǎng)站設(shè)計(jì)、日土網(wǎng)絡(luò)推廣、微信平臺(tái)小程序開(kāi)發(fā)、日土網(wǎng)絡(luò)營(yíng)銷(xiāo)、日土企業(yè)策劃、日土品牌公關(guān)、搜索引擎seo、人物專(zhuān)訪、企業(yè)宣傳片、企業(yè)代運(yùn)營(yíng)等,從售前售中售后,我們都將竭誠(chéng)為您服務(wù),您的肯定,是我們最大的嘉獎(jiǎng);創(chuàng)新互聯(lián)建站為所有大學(xué)生創(chuàng)業(yè)者提供日土建站搭建服務(wù),24小時(shí)服務(wù)熱線:13518219792,官方網(wǎng)址:www.cdcxhl.com
給你一個(gè)變量對(duì)數(shù)組 equations 和一個(gè)實(shí)數(shù)值數(shù)組 values 作為已知條件,其中 equations[i] = [Ai, Bi] 和 values[i] 共同表示等式 Ai / Bi = values[i] 。每個(gè) Ai 或 Bi 是一個(gè)表示單個(gè)變量的字符串。
另有一些以數(shù)組 queries 表示的問(wèn)題,其中 queries[j] = [Cj, Dj] 表示第 j 個(gè)問(wèn)題,請(qǐng)你根據(jù)已知條件找出 Cj / Dj = ? 的結(jié)果作為答案。
返回 所有問(wèn)題的答案 。如果存在某個(gè)無(wú)法確定的答案,則用 -1.0 替代這個(gè)答案。
注意:輸入總是有效的。你可以假設(shè)除法運(yùn)算中不會(huì)出現(xiàn)除數(shù)為 0 的情況,且不存在任何矛盾的結(jié)果。
示例 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]
解釋?zhuān)?br/>條件:a / b = 2.0, b / c = 3.0
問(wèn)題:a / c = ?, b / a = ?, a / e = ?, a / a = ?, x / x = ?
結(jié)果:[6.0, 0.5, -1.0, 1.0, -1.0 ]
示例 2:
輸入:equations = [["a","b"],["b","c"],["bc","cd"]], values = [1.5,2.5,5.0], queries = [["a","c"],["c","b"],["bc","cd"],["cd","bc"]]
輸出:[3.75000,0.40000,5.00000,0.20000]
示例 3:
輸入:equations = [["a","b"]], values = [0.5], queries = [["a","b"],["b","a"],["a","c"],["x","y"]]
輸出:[0.50000,2.00000,-1.00000,-1.00000]
提示:
1 <= equations.length <= 20
equations[i].length == 2
1 <= Ai.length, Bi.length <= 5
values.length == equations.length
0.0 < values[i] <= 20.0
1 <= queries.length <= 20
queries[i].length == 2
1 <= Cj.length, Dj.length <= 5
Ai, Bi, Cj, Dj 由小寫(xiě)英文字母與數(shù)字組成
看著很唬人,實(shí)際上建圖即可,graph里增加的是每個(gè)分式的分子和分母的對(duì)應(yīng)關(guān)系,weight里是分式的值,還有對(duì)應(yīng)的倒數(shù), visited存儲(chǔ)遍歷過(guò)的起始節(jié)點(diǎn);
DFS,查找graph里的已有分式條件,以及利用中間節(jié)點(diǎn)構(gòu)建新的分式,無(wú)法構(gòu)建則為0,查詢(xún)則返回-1.0;
from collections import defaultdict class Solution: def calcEquation(self, equations: list, values: list, queries: list) -> list: """ :type equations: List[List[str]] :type values: List[float] :type queries: List[List[str]] :rtype: List[float] """ gragh = defaultdict(set) weight = defaultdict() for i in range(len(equations)): gragh[equations[i][0]].add(equations[i][1]) gragh[equations[i][1]].add(equations[i][0]) weight[(equations[i][0], equations[i][1])] = values[i] weight[(equations[i][1], equations[i][0])] = float(1 / values[i]) print(gragh) print(weight) def dfs(st, ed, visited): if (st, ed) in weight: return weight[(st, ed)] if st not in gragh or ed not in gragh or st in visited: return 0 visited.add(st) res = 0 for link in gragh[st]: res = (dfs(link, ed, visited)) * weight[(st, link)] if res != 0: weight[(st, ed)] = res break visited.remove(st) return res res = [] for q in queries: ans = dfs(q[0], q[1], set()) if ans == 0: ans = -1.0 res.append(ans) return res if __name__ == '__main__': equations = [["a", "b"], ["b", "c"]] values = [2.0, 3.0] queries = [["a", "c"], ["b", "a"], ["a", "e"], ["a", "a"], ["x", "x"]] s = Solution() ans = s.calcEquation(equations, values, queries) print(ans)
感謝各位的閱讀,以上就是“l(fā)eetcode除法求值的方法是什么”的內(nèi)容了,經(jīng)過(guò)本文的學(xué)習(xí)后,相信大家對(duì)leetcode除法求值的方法是什么這一問(wèn)題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是創(chuàng)新互聯(lián),小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!