_241_different_ways_to_add_parentheses.py 文件源码

python
阅读 28 收藏 0 点赞 0 评论 0

项目:LeetCode 作者: YJL33 项目源码 文件源码
def diffWaysToCompute(self, s):
        """
        :type s: str
        :rtype: List[int]
        """
        tokens = re.split('(\D)', s)            # magical regular expression
        nums = map(int, tokens[::2])            # nums: an array, ops: a method (operator.xxx)
        ops = map({'+': operator.add, '-': operator.sub, '*': operator.mul}.get, tokens[1::2])
        def build(lo, hi):                      # top-down method
           if lo == hi:
               return [nums[lo]]
           return [ops[i](a, b)
                   for i in xrange(lo, hi)
                   for a in build(lo, i)
                   for b in build(i + 1, hi)]
        return build(0, len(nums)-1)
评论列表


问题


面经


文章

微信
公众号

扫码关注公众号