括号的分数

 

https://leetcode.com/problems/score-of-parentheses/solution/

(4) Score of Parentheses - LeetCode Articles
https://leetcode.com/articles/score-of-parentheses/

 

class Solution {

public int scoreOfParentheses(String S) {
int ans = 0, bal = 0;
for (int i = 0; i < S.length(); ++i) {
if (S.charAt(i) == '(') {
bal++;
} else {
bal--;
if (S.charAt(i - 1) == '(')
ans += 1 << bal;
}
}

return ans;
}
}

 

posted @ 2019-05-04 23:09  papering  阅读(244)  评论(0编辑  收藏  举报