Python recursive solution. Easy understand


  • 0
    A
    def diffWaysToCompute(self, inp):
        def helper(l, r):
            ans = []
            for i in range(l, r):
                if inp[i] not in ops:
                    continue
                ans += [ops[inp[i]](le, ri) for le in helper(l, i)
                                            for ri in helper(i + 1, r)]
            return ans if len(ans) != 0 else [int(inp[l:r])]
        ops = {
                '-': lambda x, y: x - y,
                '+': lambda x, y: x + y,
                '*': lambda x, y: x * y,
                }
        return helper(0, len(inp))

  • 0
    C

    Awesome code. I guess duplicates needs to be removed.


  • 0
    Y

    how do you handle cases where a number is more than one digit?


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.