在一个算式中添加()的方法 Different Ways to Add Parentheses

问题:

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".

((2-1)-1) = 0
(2-(1-1)) = 2

Output: [0, 2]
Example 2

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]

解决:

①  递归实现。

采用分治算法,分治算法的基本思想是将一个规模为N的问题分解为K个规模较小的子问题,这些子问题相互独立且与原问题性质相同,求出子问题的解,就可得到原问题的解。那么针对本题,以操作符为分界,将字符串分解为较小的两个子字符串,然后依次对两个子字符串进行同样的划分,直到字符串中只含有数字。再根据操作符对两端的数字进行相应的运算。

由于原问题和子问题中操作符不止有一个,那么就需要在原问题和子问题中循环找到这个操作符,进行同样的划分:

class Solution { //8ms
    public List<Integer> diffWaysToCompute(String input) {
        List<Integer> res = new ArrayList<>();
        for (int i = 0;i < input.length();i ++){
            char c = input.charAt(i);
            if (c == '+' || c == '-' || c == '*'){
                List<Integer> left = diffWaysToCompute(input.substring(0,i));
                List<Integer> right = diffWaysToCompute(input.substring(i + 1,input.length()));
                for (int l : left){
                    for (int r : right){
                        switch (c){
                            case '+':
                                res.add(l + r);
                                break;
                            case '-':
                                res.add(l - r);
                                break;
                            case '*':
                                res.add(l * r);
                                break;
                        }
                    }
                }
            }
        }
        if (res.size() == 0) res.add(Integer.valueOf(input));//?
        return res;
    }
}

② 采用备忘录的自顶向下法,将子问题的计算结果保存下来,下次遇到同样的子问题就直接从备忘录中取出,而免去繁琐的计算,具体的做法是新建一个 hashmap,将子字符串放入 hashmap 中,对应的计算结果放入 value 中。

class Solution { //3ms
    Map<String,List<Integer>> map = new HashMap<>();
    public List<Integer> diffWaysToCompute(String input) {
        if (map.containsKey(input)) return map.get(input);
        List<Integer> res = new ArrayList<>();
        for (int i = 0;i < input.length();i ++){
            char c = input.charAt(i);
            if (c == '+' || c == '-' || c == '*'){
                for (int l : diffWaysToCompute(input.substring(0,i))){
                    for (int r : diffWaysToCompute(input.substring(i + 1,input.length()))){
                        if (c == '+'){
                            res.add(l + r);
                        }else if (c == '-'){
                            res.add(l - r);
                        }else{
                            res.add(l * r);
                        }
                    }
                }
            }
        }
        if (res.size() == 0) res.add(Integer.valueOf(input));
        map.put(input,res);
        return res;
    }
}

转载于:https://my.oschina.net/liyurong/blog/1591627

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值