Leetcode 399

给出了一些等式,比如a/b, b/c,求a/c, a/e, f/f等这些式子的结果。

 

sol:

考虑建图,然后用Floyd算法。。

 

    Map<String, Integer> map = new HashMap<>();
    int index = 0;

    private int getIndex(String s) {
        Integer val = map.get(s);
        if (val == null) {
            map.put(s, index++);
            return index - 1;
        } else return val;
    }

    public static void main(String[] args) {
        Leetcode0399 sol = new Leetcode0399();
        List<List<String>> equations = new ArrayList<>();
        equations.add(Lists.newArrayList("a", "b"));
        equations.add(Lists.newArrayList("b", "c"));
        double[] values = new double[]{2.0, 3.0};
        List<List<String>> queries = new ArrayList<>();
        queries.add(Lists.newArrayList("a", "c"));
        queries.add(Lists.newArrayList("b", "a"));
        queries.add(Lists.newArrayList("a", "e"));
        queries.add(Lists.newArrayList("a", "a"));
        queries.add(Lists.newArrayList("x", "x"));
        sol.calcEquation(equations, values, queries);
    }

    public double[] calcEquation(List<List<String>> equations, double[] values, List<List<String>> queries) {
        double[] res = new double[queries.size()];
        int maIndex = 0;
        Set<String> set = new HashSet<>();

        for (int i = 0; i < equations.size(); i++) {
            List<String> strings = equations.get(i);
            String a = strings.get(0);
            String b = strings.get(1);
            set.add(a);
            set.add(b);
        }
        int max = set.size();
        double[][] graph = new double[max][max];
        for (int i = 0; i < graph.length; i++) {
            for (int j = 0; j < graph.length; j++) {
                graph[i][j] = -1.0;
            }
        }

        for (int i = 0; i < equations.size(); i++) {
            List<String> strings = equations.get(i);
            String a = strings.get(0);
            int ai = getIndex(a);
            String b = strings.get(1);
            int bi = getIndex(b);
            graph[ai][ai] = 1;
            graph[bi][bi] = 1;
            graph[ai][bi] = values[i];
            graph[bi][ai] = 1.0 / values[i];
        }
        maIndex = index;

        for (int k = 0; k < maIndex; k++) {
            for (int i = 0; i < maIndex; i++) {
                for (int j = 0; j < maIndex; j++) {
                    if (graph[i][k] > 0 && graph[k][j] > 0) {
                        graph[i][j] = graph[i][k] * graph[k][j];
                    }
                }
            }
        }

        for (int i = 0; i < queries.size(); i++) {
            List<String> strings = queries.get(i);
            Integer a = map.get(strings.get(0));
            Integer b = map.get(strings.get(1));
            if (a != null && b != null && a < maIndex && b < maIndex) {
                res[i] = graph[a][b];
            } else {
                res[i] = -1.0;
            }
        }
//        System.out.println(Arrays.toString(res));
        return res;
    }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值