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

SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。

(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;
}
}

 

扫码关注我们
微信号:SRE实战
拒绝背锅 运筹帷幄