问题

Given a string of numbers and operators, return all possible results from computing all the different possible ways to group numbers and operators. The valid operators are +, - and *

Example

Input: 2*3-4*5

(2*(3-(4*5))) = -34
((2*3)-(4*5)) = -14
((2*(3-4))*5) = -10
(2*((3-4)*5)) = -10
(((2*3)-4)*5) = 10

Output: [-34, -14, -10, -10, 10]

思路

  • 解析 token,parse

  • 驱动方法,diffWaysToCompute(String)

  • 递归求解 + 缓存,diffWaysToCompute(String[], int, int)

提交

public class Solution {

    private List<Integer>[][] cache;

    public List<Integer> diffWaysToCompute(String input) {
        String[] tokens = parse(input);
        cache = new List[tokens.length][tokens.length];
        for (int i = 0; i < tokens.length; i++) {
            cache[i] = new List[tokens.length];
        }

        return diffWaysToCompute(tokens, 0, tokens.length - 1);
    }
    
    public String[] parse(String input) {
        ArrayList<String> tokens = new ArrayList<String>();
        int i = 0;
        while (i < input.length()) {
            if (input.charAt(i) == '+') {
                tokens.add("+");
                i++;
            } else if (input.charAt(i) == '-') {
                tokens.add("-");
                i++;
            } else if (input.charAt(i) == '*') {
                tokens.add("*");
                i++;
            } else {
                StringBuilder builder = new StringBuilder();
                while (i < input.length() && input.charAt(i) >= '0' && input.charAt(i) <= '9') {
                    builder.append(input.charAt(i));
                    i++;
                }
                tokens.add(builder.toString());
            }
        }
        String[] result = new String[tokens.size()];
        result = tokens.toArray(result);
        return result;
    }

    public List<Integer> diffWaysToCompute(String[] tokens, int from, int to) {
        if (from > to) {
            return Collections.emptyList();
        }

        if (from == to) {
            return Collections.singletonList(Integer.parseInt(tokens[from]));
        }

        if (cache[from][to] != null) {
            return cache[from][to];
        }

        int length = tokens.length;
        List<Integer> result = new ArrayList<Integer>();
        for (int i = from; i <= to; i++) {
            if (tokens[i].equals("+") || tokens[i].equals("-") || tokens[i].equals("*")) {
                List<Integer> left = diffWaysToCompute(tokens, from, i - 1);
                List<Integer> right = diffWaysToCompute(tokens, i + 1, to);

                for (Integer aLeft : left) {
                    for (Integer aRight : right) {
                        if (tokens[i].equals("+")) {
                            result.add(aLeft + aRight);
                        } else if (tokens[i].equals("-")) {
                            result.add(aLeft - aRight);
                        } else if (tokens[i].equals("*")) {
                            result.add(aLeft * aRight);
                        }
                    }
                }
            }
        }

        cache[from][to] = result;

        return result;
    }
}

xingpingz
122 声望64 粉丝

博学,审问,慎思,明辨,力行