856. 括号的分数
856. Score of Parentheses
题目描述
给定一个平衡括号字符串?S,按下述规则计算该字符串的分数:
LeetCode856. Score of Parentheses中等
示例 1:
示例 2:
示例?3:
示例?4:
提示:
Java 实现
import java.util.Stack;
class Solution {
public int scoreOfParentheses(String S) {
int cur = 0;
Stack<Integer> stack = new Stack<>();
for (char c : S.toCharArray()) {
if (c == '(') {
stack.push(cur);
cur = 0;
} else {
cur = stack.pop() + Math.max(cur * 2, 1);
}
}
return cur;
}
}
参考资料
LeetCode 856. 括号的分数(Score of Parentheses)
原文:https://www.cnblogs.com/hglibin/p/10988617.html