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<Integer> diffWaysToCompute(String input) {
        if (input.isEmpty()) {
            return new ArrayList<>();
        }

      	// 先将字符串拆分成操作数集合和操作符集合,便于处理
        List<Character> ops = new ArrayList<>();
        List<Integer> 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<Integer> diff(List<Integer> nums, List<Character> ops, int left, int right) {
        List<Integer> list = new ArrayList<>();

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

        for (int mid = left; mid < right; mid++) {
            List<Integer> part1 = diff(nums, ops, left, mid);
            List<Integer> 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<Integer> diffWaysToCompute(String input) {
        return diffWaysToCompute(input, new HashMap<>());
    }

    private List<Integer> diffWaysToCompute(String input, Map<String, List<Integer>> record) {
        List<Integer> 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<Integer> left = diffWaysToCompute(input.substring(0, i), record);
                List<Integer> 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;
    }
}
posted @ 2020-06-19 11:35  墨云黑  阅读(252)  评论(0编辑  收藏  举报