0241. Different Ways to Add Parentheses (M)

Different Ways to Add Parentheses (M)

题目

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 1:

Input: "2-1-1"
Output: [0, 2]
Explanation: 
((2-1)-1) = 0 
(2-(1-1)) = 2

Example 2:

Input: "2*3-4*5"
Output: [-34, -14, -10, -10, 10]
Explanation: 
(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

题意

给定一串数字表达式,可以任意添加括号,求所有可能得到的结果。

思路

难点在于如何添加括号来得到不同的表达式。可以这样考虑,每一个算术表达式都可以拆分成三个部分,左操作数、操作符、右操作数,即 \((...)\ ?\ (...)\) 的形式,只要改变每个括号内操作数的个数,得到的一定是不一样的括号形式;而每一个括号里又是一个算数表达式,可以递归的进行处理,最终就能得到所有括号的组合形式。


代码实现

Java

分治法

class Solution {
    public List diffWaysToCompute(String input) {
        if (input.isEmpty()) {
            return new ArrayList<>();
        }

      	// 先将字符串拆分成操作数集合和操作符集合,便于处理
        List ops = new ArrayList<>();
        List nums = new ArrayList<>();
        int num = 0;
        for (char c : input.toCharArray()) {
            if (Character.isDigit(c)) {
                num = num * 10 + c - '0';
            } else {
                ops.add(c);
                nums.add(num);
                num = 0;
            }
        }
        nums.add(num);

        return diff(nums, ops, 0, nums.size() - 1);
    }

    private List diff(List nums, List ops, int left, int right) {
        List list = new ArrayList<>();

        if (left == right) {
            list.add(nums.get(left));
            return list;
        }

        for (int mid = left; mid < right; mid++) {
            List part1 = diff(nums, ops, left, mid);
            List part2 = diff(nums, ops, mid + 1, right);
            for (int i = 0; i < part1.size(); i++) {
                for (int j = 0; j < part2.size(); j++) {
                    int num1 = part1.get(i), num2 = part2.get(j);
                    char op = ops.get(mid);
                    list.add(op == '-' ? num1 - num2 : op == '+' ? num1 + num2 : num1 * num2);
                }
            }
        }

        return list;
    }
}

记忆化优化

class Solution {
    public List diffWaysToCompute(String input) {
        return diffWaysToCompute(input, new HashMap<>());
    }

    private List diffWaysToCompute(String input, Map> record) {
        List list = new ArrayList<>();

        if (input.isEmpty())
            return list;
        if (record.containsKey(input))
            return record.get(input);

        for (int i = 0; i < input.length(); i++) {
          	// 根据操作符划分左右
            if (!Character.isDigit(input.charAt(i))) {
                char op = input.charAt(i);
                List left = diffWaysToCompute(input.substring(0, i), record);
                List right = diffWaysToCompute(input.substring(i + 1), record);
                for (int p = 0; p < left.size(); p++) {
                    for (int q = 0; q < right.size(); q++) {
                        int num1 = left.get(p), num2 = right.get(q);
                        list.add(op == '-' ? num1 - num2 : op == '+' ? num1 + num2 : num1 * num2);
                    }
                }
            }
        }

        // list为空,说明不存在操作符,input为完整数字
        if (list.size() == 0) {
            list.add(Integer.parseInt(input));
        }
        
        record.put(input, list);
        return list;
    }
}

你可能感兴趣的:(0241. Different Ways to Add Parentheses (M))