evaluate-division

https://leetcode.com/problems/evaluate-division/

public class Solution {
    private Map mp;
    private class Item {
        public String str;
        public double prop;
        public Item(String s, double p) {
            str = s;
            prop = p;
        }
    }

    public double[] calcEquation(String[][] equations, double[] values, String[][] queries) {
        mp = new HashMap();

        for (int i=0; i<values.length; i++) {
            String str1 = equations[i][0];
            String str2 = equations[i][1];
            double val = values[i];

            List lt = (List)mp.remove(str2);
            if (lt == null) {
                lt = new ArrayList();
            }
            Item itm = new Item(str1, val);
            lt.add(itm);
            mp.put(str2, lt);

            lt = (List)mp.remove(str1);
            if (lt == null) {
                lt = new ArrayList();
            }
            itm = new Item(str2, 1/val);
            lt.add(itm);
            mp.put(str1, lt);
        }

        double []ret = new double[queries.length];
        Set st = new HashSet();
        Queue qe = new LinkedList();

        Iterator itr;
        List lt;
        Item baseItm;

        for (int i=0; i<queries.length; i++) {
            st.clear();
            qe.clear();

            double dret = -1;
            String str1 = queries[i][0];
            String str2 = queries[i][1];

            qe.offer(new Item(str2, 1));
            st.add(str2);

            while ((baseItm = (Item)qe.poll()) != null) {

                lt = (List)mp.get(baseItm.str);
                if (lt == null) {
                    continue;
                }

                itr = lt.iterator();

                while (itr.hasNext()) {

                    Item itmm = (Item)itr.next();
                    if (itmm.str.equals(str1)) {
                        dret = itmm.prop * baseItm.prop;
                        break;
                    }

                    if (st.contains(itmm.str)) {
                        continue;
                    }
                    Item newItm = new Item(itmm.str, itmm.prop*baseItm.prop);
                    qe.offer(newItm);
                    st.add(itmm.str);
                }

                if (dret != -1) {
                    break;
                }
            }

            ret[i] = dret;
        }

        return ret;

    }
}
时间: 2024-10-15 00:35:05

evaluate-division的相关文章

399. Evaluate Division

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 =

【Graph】399. Evaluate Division(Medium)

#week3# #from leetcode# Description 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,

[LeetCode] 399. Evaluate Division Java

题目: 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 /

[leetcode]Graph-399. Evaluate Division

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 =

LeetCode 399. Evaluate Division

原题链接在这里:https://leetcode.com/problems/evaluate-division/description/ 题目: 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 a

过中等难度题目.0310

  .   8  String to Integer (atoi)    13.9% Medium   . 151 Reverse Words in a String      15.7% Medium     . 288 Unique Word Abbreviation      15.8% Medium     . 29 Divide Two Integers      16.0% Medium     . 166 Fraction to Recurring Decimal      17.

继续过中等难度.0309

  .   8  String to Integer (atoi)    13.9% Medium   . 151 Reverse Words in a String      15.7% Medium     . 288 Unique Word Abbreviation      15.8% Medium     . 29 Divide Two Integers      16.0% Medium     . 166 Fraction to Recurring Decimal      17.

LeetCode Problems List 题目汇总

No. Title Level Rate 1 Two Sum Medium 17.70% 2 Add Two Numbers Medium 21.10% 3 Longest Substring Without Repeating Characters Medium 20.60% 4 Median of Two Sorted Arrays Hard 17.40% 5 Longest Palindromic Substring Medium 20.70% 6 ZigZag Conversion Ea

Leetcode problems classified by company 题目按公司分类(Last updated: October 2, 2017)

Sorted by frequency of problems that appear in real interviews.Last updated: October 2, 2017Google (214)534 Design TinyURL388 Longest Absolute File Path683 K Empty Slots340 Longest Substring with At Most K Distinct Characters681 Next Closest Time482

[总结]图

在图的基本算法中,最初需要接触的就是图的遍历算法,根据访问节点的顺序,可分为广度优先搜索(BFS)和深度优先搜索(DFS).深度优先搜索,顾名思义即为一条道走到黑的搜索策略,行不通退回来换另外一条道再走到黑,依次直到搜索完成.其过程简要来说是对每一个可能的分支路径深入到不能再深入为止,而且每个节点只能访问一次.宽度优先搜索算法(又称广度优先搜索)是最简便的图的搜索算法之一,这一算法也是很多重要的图的算法的原型.Dijkstra单源最短路径算法和Prim最小生成树算法都采用了和宽度优先搜索类似的思