美文网首页
241. Different Ways to Add Paren

241. Different Ways to Add Paren

作者: 一个想当大佬的菜鸡 | 来源:发表于2019-06-26 09:31 被阅读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
             

相关文章

网友评论

      本文标题:241. Different Ways to Add Paren

      本文链接:https://www.haomeiwen.com/subject/bljqcctx.html