Leetcode

856. Score of Parentheses

YGSEO 2020. 8. 7. 23:01
728x90

leetcode.com/problems/score-of-parentheses/

class Solution:
    def scoreOfParentheses(self, S: str) -> int:
        if not S:
            return 0

        count = 0
        stack = []
        flag = 0
        for i in range(len(S)):
            if S[i] == "(":
                flag = 1
                stack.append("(")
            if S[i] == ")":
                if flag == 1:
                    count += 2**(len(stack)-1)
                    flag = 0                
                stack.pop()
        return count

출처: leetcode.com/problems/score-of-parentheses/discuss/354283/Python-using-stack

 

 

 

728x90