方法
直接便利,记录深度,然后算分。
class Solution {
public int scoreOfParentheses(String s) {
int score = 0;
char[] arr = s.toCharArray();
int depth = -1;
for (int i = 0; i < arr.length; i++) {
if (arr[i] == '(') {
depth++; // 进入深的一层
} else if (i >= 1 && arr[i] == ')') {
if (arr[i - 1] == '(') {
score += Math.pow(2, depth); // (),根据depth算score
depth--;
} else if (arr[i - 1] == ')') {// ))意味着一层结束
depth--;
}
}
}
return score;
}
}