每日一道Leetcode - 856. 括号的分数 【栈】

在这里插入图片描述

class Solution(object):
    def scoreOfParentheses(self, S):
        """
        :type S: str
        :rtype: int
        """
        stack = []
        for x in S:
            if x == '(':
                stack.append('(')
            else:
                if(stack[-1]=='('):
                    a = stack.pop()
                    stack.append(1)
                else:
                    a = stack.pop()
                    temp = 0
                    while(a!='('):
                        temp+=a
                        a = stack.pop()
                    stack.append(2*temp)
        res = sum(stack)
        return res
        
已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 1024 设计师:白松林 返回首页