给定一个含有数字和运算符的字符串,为表达式添加括号

给定一个含有数字和运算符的字符串,为表达式添加括号,改变其运算优先级以求出不同的结果。你需要给出所有可能的组合的结果。有效的运算符号包含 +, - 以及 * 。

示例 1:

输入: "2-1-1"
输出: [0, 2]
解释: 
((2-1)-1) = 0 
(2-(1-1)) = 2
示例 2:

输入: "2*3-4*5"
输出: [-34, -14, -10, -10, 10]
解释: 
(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


来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/different-ways-to-add-parentheses
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

 public static void main(String[] args) {
        //分治法,碰到运算符号,递归求解前一半的值和后一半的值,然后根据运算符号,计算两者构成的值
        List<Integer> a = diffWaysToCompute("2*3-4*5");
        System.out.println(a);
    }

    public static List<Integer> diffWaysToCompute(String input) {

        List<Integer> res = new LinkedList<>();

        for (int i = 0; i < input.length(); i++) {

            char ch = input.charAt(i);
            if (ch == '+' || ch == '-' || ch == '*') {

                List<Integer> left = diffWaysToCompute(input.substring(0, i));
                List<Integer> right = diffWaysToCompute(input.substring(i + 1));

                for (int l : left) {

                    for (int r : right) {

                        if (ch == '+')
                            res.add(l + r);
                        else if (ch == '-')
                            res.add(l - r);
                        else
                            res.add(l * r);
                    }
                }
            }
        }
        if (res.size() == 0)
            res.add(Integer.parseInt(input));
        return res;
    }
发布了88 篇原创文章 · 获赞 33 · 访问量 19万+

猜你喜欢

转载自blog.csdn.net/ccmedu/article/details/102263822