# leetcode 856. Score of Parentheses （python）

·  阅读 359

### 描述

Given a balanced parentheses string s, return the score of the string.

The score of a balanced parentheses string is based on the following rule:

• "()" has score 1.
• AB has score A + B, where A and B are balanced parentheses strings.
• (A) has score 2 * A, where A is a balanced parentheses string.

Example 1:

``````Input: s = "()"
Output: 1

Example 2:

``````Input: s = "(())"
Output: 2

Example 3:

``````Input: s = "()()"
Output: 2

Example 4:

``````Input: s = "(()(()))"
Output: 6

Example 5:

Note:

``````2 <= s.length <= 50
s consists of only '(' and ')'.
s is a balanced parentheses string.

### 解析

• 对于 () 分数为 1
• 对于 A+B 型的组合，分数为 A 的分数加上 B 的分数
• 对于 (A) 型的组合，分数为 A 的分数的 2 倍

• [0] init stack
• [0, 0] after parsing (
• [0, 0, 0] after (
• [0, 1] after )
• [0, 1, 0] after (
• [0, 1, 0, 0] after (
• [0, 1, 1] after )
• [0, 3] after )
• [6] after )

### 解答

``````class Solution(object):
def scoreOfParentheses(self, s):
"""
:type s: str
:rtype: int
"""
stack = [0]
for c in s:
if c == '(':
stack.append(0)
elif c == ')':
t = stack.pop()
stack[-1] += max(2*t, 1)
return stack[-1]

### 运行结果

``````Runtime: 31 ms, faster than 5.22% of Python online submissions for Score of Parentheses.
Memory Usage: 13.5 MB, less than 59.70% of Python online submissions for Score of Parentheses.

### 解析

• 其中出现的第一个出现的 () 深度为 1 ，其分数为 1>>1
• 第二个出现的 () 深度 2 ，其分数为 1>>2
• 将两者加起来即为答案 6

### 解答

``````class Solution(object):
def scoreOfParentheses(self, s):
"""
:type s: str
:rtype: int
"""
result, balance = 0, 0
for i,c in enumerate(s):
if c=='(':
balance += 1
else:
balance -= 1
if s[i-1] == '(':
result += 1 << balance
return result

### 运行结果

``````Runtime: 19 ms, faster than 42.54% of Python online submissions for Score of Parentheses.
Memory Usage: 13.4 MB, less than 59.70% of Python online submissions for Score of Parentheses.