Description
Given a balanced parentheses string S, compute the score of the string 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:
Output:
Example 2:
Input:
Output:
Example 3:
Input:
Output:
Example 4:
Input:
Output:
Note:
- S is a balanced parentheses string, containing only ( and ).
- 2 <= S.length <= 50
分析
题目的意思是:给你一个合法的括号字符串,按照题目给定的规则来计算这个字符串的得分。
- 这种括号字符串可以用递归实现,写着还是不难,按照上面给的三条规则写就完事了,递归的终止条件是“()”,然后返回1就行了,其他的根据规则写就行了。
代码
参考文献