其他分享
首页 > 其他分享> > 每日一道Leetcode - 856. 括号的分数 【栈】

每日一道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
        

标签:856,temp,res,pop,stack,括号,else,Leetcode,append
来源: https://blog.csdn.net/weixin_41041275/article/details/113108532