856. Score of Parentheses

856. Score of Parentheses

class Solution:
    def scoreOfParentheses(self, s: str) -> int:
        stack=[]
        i=0
        for c in s:
            if c=='(':
                stack.append(c)
            else:
                score=0
                while stack[-1]!='(':
                    score+=stack.pop()
                stack.pop()
                score= score*2 if score!=0 else 1
                stack.append(score)
        return sum(stack)
            

你可能感兴趣的:(leetcode)