241. Different Ways to Add Paren

2019-06-26  本文已影响0人  一个想当大佬的菜鸡

241. Different Ways to Add Parentheses

241. Different Ways to Add Parentheses
class Solution(object):
    def diffWaysToCompute(self, input):
        """
        :type input: str
        :rtype: List[int]
        """
        res = []
        for i, v in enumerate(input):
            if v in "+-*":
                for l1 in self.diffWaysToCompute(input[:i]):
                    for l2 in self.diffWaysToCompute(input[i+1:]):
                        if v == '+':
                            res.append(l1 + l2)
                        elif v == "-":
                            res.append(l1 - l2)
                        else:
                            res.append(l1 * l2)
        if len(res) == 0:
            return [int(input)]
        return res
             
上一篇 下一篇

猜你喜欢

热点阅读