摘要:題目要求已知一些字母之間的關(guān)系式,問是否能夠計(jì)算出其它字母之間的倍數(shù)關(guān)系如已知問是否能夠計(jì)算出的值。這里的值因?yàn)樵跅l件中無法獲知是否等于零,因此也無法計(jì)算其真實(shí)結(jié)果,也需要返回。即代表點(diǎn)指向點(diǎn)的邊的權(quán)重為,而點(diǎn)指向點(diǎn)的邊的全中為。
題目要求
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.
已知一些字母之間的關(guān)系式,問是否能夠計(jì)算出其它字母之間的倍數(shù)關(guān)系?
如已知a/b=2.0 b/c=3.0問是否能夠計(jì)算出a/c, b/a, a/e, a/a, x/x的值。如果無法計(jì)算得出,則返回-1。這里x/x的值因?yàn)樵跅l件中無法獲知x是否等于零,因此也無法計(jì)算其真實(shí)結(jié)果,也需要返回-1。
假如我們將除數(shù)和被除數(shù)看做是圖的頂點(diǎn),將除數(shù)和被除數(shù)之間的倍數(shù)關(guān)系試做二者之間邊的權(quán)重。即a/b=2.0代表點(diǎn)a指向點(diǎn)b的邊的權(quán)重為2.0,而點(diǎn)b指向點(diǎn)a的邊的全中為1/2.0=0.5。
因此我們可以將輸入的表達(dá)式轉(zhuǎn)化為一個(gè)加權(quán)有向圖,而題目的問題則被轉(zhuǎn)化為求兩個(gè)點(diǎn)之間是否能夠找到一條邊,如果無法找到,則返回-1,否則返回路徑上每條邊的權(quán)重的乘積。
代碼如下:
public double[] calcEquation(List> equations, double[] values, List
> queries) { //圖的鏈?zhǔn)奖硎痉? Map
> pairs = new HashMap<>(); //圖上每條邊的權(quán)重 Map > valuedPairs = new HashMap<>(); for(int i = 0 ; i < equations.size() ; i++) { //獲取第i個(gè)方程式 List equation = equations.get(i); String multiplied = equation.get(0);//被除數(shù) String multiplier = equation.get(1);//除數(shù) //如果被除數(shù)從來沒有添加到圖中,則將其作為頂點(diǎn)在圖中初始化 if(!pairs.containsKey(multiplied)) { pairs.put(multiplied, new ArrayList<>()); valuedPairs.put(multiplied, new ArrayList<>()); } //如果除數(shù)從來沒有添加到圖中,則將其作為頂點(diǎn)在圖中初始化 if(!pairs.containsKey(multiplier)) { pairs.put(multiplier, new ArrayList<>()); valuedPairs.put(multiplier, new ArrayList<>()); } //添加邊和邊的權(quán)重 pairs.get(multiplied).add(multiplier); pairs.get(multiplier).add(multiplied); valuedPairs.get(multiplied).add(values[i]); valuedPairs.get(multiplier).add(1.0 / values[i]); } //結(jié)果集 double[] result = new double[queries.size()]; for(int i = 0 ; i (), 1.0); result[i] = result[i]==0.0 ? -1.0 : result[i]; } return result; } public double dfs(String multiplied, String multiplier, Map > pairs, Map > valuedPairs, Set visited, double curResult) { //如果圖中不包含該被除數(shù)頂點(diǎn),則無法獲知該表達(dá)式的值 if(!pairs.containsKey(multiplied)) return 0.0; //如果再次訪問過該被除數(shù),說明找到了一條環(huán)路,則該深度優(yōu)先遍歷結(jié)果失敗,直接拋棄 if(visited.contains(multiplied)) return 0.0; //如果被除數(shù)等于除數(shù),則返回1.0 if(multiplied.equals(multiplier)) return curResult; visited.add(multiplied); //獲得當(dāng)前被除數(shù)的所有鄰接頂點(diǎn) List multipliers = pairs.get(multiplied); //獲得所有鄰接邊的權(quán)重 List multiplierValues = valuedPairs.get(multiplied); double tmp = 0.0; for(int i = 0 ; i
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/74416.html
Problem 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 ...
摘要:建好圖之后就是查找了,圖里面查找用或者都可以,寫起來簡(jiǎn)單點(diǎn)。復(fù)雜度沒什么差別都是,這道題里面最多是,所以每次查找的復(fù)雜度是,有次查找。注意防止重復(fù)路徑,要用。 399. Evaluate Division 題目鏈接:https://leetcode.com/problems... 無向圖里找路徑的問題,用鄰接鏈或者鄰接矩陣來建圖,用鄰接鏈的話注意兩個(gè)方向,a/b的時(shí)候,既要把b加到a的...
Problem Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, *, /. Each operand may be an integer or another expression. Note: Division between two inte...
摘要:題目根據(jù)逆波蘭表示法,求表達(dá)式的值。給定逆波蘭表達(dá)式總是有效的。逆波蘭表達(dá)式又叫做后綴表達(dá)式。解題思路可以看出逆波蘭表達(dá)式中的每一個(gè)運(yùn)算符屬于該運(yùn)算符前的兩個(gè)數(shù)字間的運(yùn)算。如如波蘭表達(dá)式則加號(hào)前兩個(gè)數(shù)字為。 題目: 根據(jù)逆波蘭表示法,求表達(dá)式的值。 有效的運(yùn)算符包括 +, -, *, / 。每個(gè)運(yùn)算對(duì)象可以是整數(shù),也可以是另一個(gè)逆波蘭表達(dá)式。 Evaluate the value of...
摘要:小鹿題目根據(jù)逆波蘭表示法,求表達(dá)式的值。給定逆波蘭表達(dá)式總是有效的。算法思路仔細(xì)觀察上述的逆波蘭表達(dá)式,可以發(fā)現(xiàn)一個(gè)規(guī)律就是每遇到一個(gè)操作符,就將操作符前的兩個(gè)操作數(shù)進(jìn)行運(yùn)算,將結(jié)果保存到原位置。 Time:2019/4/14Title: Evaluate Reverse Polish NotationDifficulty: MediumAuthor:小鹿 題目:Evaluate ...
閱讀 2424·2021-09-08 09:45
閱讀 3364·2021-09-08 09:45
閱讀 3111·2019-08-30 15:54
閱讀 3361·2019-08-26 13:54
閱讀 1418·2019-08-26 13:26
閱讀 1396·2019-08-26 13:23
閱讀 920·2019-08-23 17:57
閱讀 2190·2019-08-23 17:14