资讯专栏INFORMATION COLUMN

leetcode241. Different Ways to Add Parentheses

since1986 / 606人阅读

摘要:思路与算法这是经典的分治法。我们将算式从一个操作符的位置分割开,并分别得出左边和右边算式所有可能的值。再将左右的值分别按照操作符进行计算。将已经遍历过的结果存入缓存中,如果碰到重复的计算式,就可以直接获取其所有可能的值。

题目要求
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]

现在有一个字符串形式的算式,求该算式在任意位置加上任意数量的括号后能够得出的所有可能的值。

思路与算法

这是经典的分治法。我们将算式从一个操作符的位置分割开,并分别得出左边和右边算式所有可能的值。再将左右的值分别按照操作符进行计算。这里通过递归实现。

    public List diffWaysToCompute(String input) {
        return diffWaysToCompute(input, 0, input.length());
    }
    
    public List diffWaysToCompute(String input, int startIndex, int endIndex){
        boolean isDigit = true;
        List result = new ArrayList();
        for(int i = startIndex ; i leftValue = diffWaysToCompute(input, startIndex, i);
                List rightValue = diffWaysToCompute(input, i+1, endIndex);
                result.addAll(compute(leftValue, rightValue,cur));
            }
        }
        if(isDigit){
            result.add(Integer.parseInt(input.substring(startIndex, endIndex)));
        }
        return result;
    }
    
    public List compute(List leftValue, List rightValue, char operator){
        switch(operator){
        case "+" : return add(leftValue, rightValue);
        case "-" : return minus(leftValue, rightValue);
        case "*" : return multiply(leftValue, rightValue);
        }
        return new ArrayList<>();
    }
    
    public List add(List leftValue, List rightValue){
        List result = new ArrayList();
        for(int left : leftValue){
            for(int right : rightValue){
                result.add(left + right);
            }
        }
        return result;
    }
    
    public List minus(List leftValue, List rightValue){
        List result = new ArrayList();
        for(int left : leftValue){
            for(int right : rightValue){
                result.add(left - right);
            }
        }
        return result;
    }
    
    public List multiply(List leftValue, List rightValue){
        List result = new ArrayList();
        for(int left : leftValue){
            for(int right : rightValue){
                result.add(left * right);
            }
        }
        return result;
    }

提高性能的方法是通过Map实现缓存。将已经遍历过的结果存入缓存中,如果碰到重复的计算式,就可以直接获取其所有可能的值。

    Map> cache = new HashMap>();
    public List diffWaysToCompute_withCache(String input){
        if(cache.containsKey(input)) return cache.get(input);
        List result = new ArrayList();
        boolean isDigit = true;
        for(int i = 0 ; i leftValues = diffWaysToCompute_withCache(input.substring(0, i));
                List rightValues = diffWaysToCompute_withCache(input.substring(i+1));
                for(Integer left : leftValues){
                    for(Integer right : rightValues){
                        switch(cur){
                        case "+" : result.add(left + right); break;
                        case "-" : result.add(left - right); break;
                        case "*" : result.add(left * right); break;
                        }
                    }
                }
            }
        }
        if(isDigit){ result.add(Integer.parseInt(input));}
        cache.put(input, result);
        return result;
    }


想要了解更多开发技术,面试教程以及互联网公司内推,欢迎关注我的微信公众号!将会不定期的发放福利哦~

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/61940.html

相关文章

  • leetcode241. Different Ways to Add Parentheses

    摘要:思路与算法这是经典的分治法。我们将算式从一个操作符的位置分割开,并分别得出左边和右边算式所有可能的值。再将左右的值分别按照操作符进行计算。将已经遍历过的结果存入缓存中,如果碰到重复的计算式,就可以直接获取其所有可能的值。 题目要求 Given a string of numbers and operators, return all possible results from comp...

    xi4oh4o 评论0 收藏0
  • leetcode22. Generate Parentheses

    摘要:当右括号和左括号的剩余量均为时,及为一个最终结果。而则会在直接原来的对象上进行修改,其指针仍然指向原来的对象。因此在递归的过程中使用一定要注意,对对象的修改不要相互干扰。 题目要求 Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses....

    骞讳护 评论0 收藏0
  • LeetCode[22] Generate Parentheses

    摘要:复杂度思路注意的地方,要限制左括号和右括号。每出现一次左括号,就相对于限定了,最多只能出现那么多右括号。所以,为了完成这种限定,用来控制。不然会有的情况出现。 LeetCode[22] Generate Parentheses Given n pairs of parentheses, write a function to generate all combinations of ...

    Jonathan Shieber 评论0 收藏0
  • [Leetcod] Generate Parentheses 产生括号

    摘要:而对于右括号,必须前面放了一个左括号,我们才能放一个右括号。所以我们根据剩余可放置左括号,和剩余可放置右括号,代入递归,就可以求解。 Generate Parentheses 最新更新请见:https://yanjia.me/zh/2019/01/... Given n pairs of parentheses, write a function to generate all co...

    Ilikewhite 评论0 收藏0
  • [LeetCode] 22. Generate Parentheses

    Problem Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. For example, given n = 3, a solution set is: [ ((())), (()()), (())(), ()(()), ...

    curlyCheng 评论0 收藏0

发表评论

0条评论

since1986

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<