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.pswp.cn/news/392362.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/392362.shtml
英文地址,請注明出處:http://en.pswp.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…