본문 바로가기
Leetcode

856. Score of Parentheses

by YGSEO 2020. 8. 7.
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

'Leetcode' 카테고리의 다른 글

1025. Divisor Game  (0) 2020.08.10
856. Score of Parentheses  (0) 2020.08.10
1190. Reverse Substrings Between Each Pair of Parentheses  (0) 2020.08.05
1249. Minimum Remove to Make Valid Parentheses  (0) 2020.08.04
739. Daily Temperatures  (0) 2020.08.03

댓글