Python 192ms solution with Stack


  • 1
    K
    class Solution(object):
        def calculate(self, s):
            nums = []
            sign = 1
            num = 0
            rst = 0
        
            for c in s:
                if c.isdigit():
                    num = num*10 + int(c)
                    continue
                rst += sign*num
                num = 0
            
                if c == "-": sign = -1
                elif c == "+": sign = 1
                elif c == "(":
                    nums.append(rst)
                    nums.append(sign)
                    sign = 1
                    rst = 0
                elif c == ")":
                    rst *= nums.pop()
                    rst += nums.pop()
    
            return rst + sign * num

Log in to reply
 

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