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: 1

Example 2:

Input: "(())"
Output: 2

Example 3:

Input: "()()"
Output: 2

Example 4:

Input: "(()(()))"
Output: 6

Note:

  1. S is a balanced parentheses string, containing only ( and ).
  2. 2 <= S.length <= 50

Runtime: 0 ms, faster than 100.00% of C++ online submissions for Score of Parentheses.

(A) = 2 * A 是表示深度的一个概念。

class Solution {
public:
int scoreOfParentheses(string S) {
int ret = , cnt = ;
char last = ' ';
for(auto ch : S){
if(ch == '('){
cnt++;
}else {
cnt--;
if(last == '('){
ret += (<<cnt);
}
}
last = ch;
}
return ret;
}
};

最新文章

  1. ENode 1.0 - Saga的思想与实现
  2. LinkIssue: Error &#39;LINK : fatal error LNK1123: failure during conversion to COFF: file invalid or cor
  3. 软件快速开发平台 JEPF
  4. 怎样用ZBrush快速雕刻皮肤纹理
  5. codeforces 468B 2-sat
  6. sourceInsight的技巧
  7. jquery事件链式写法
  8. c#委托实例化和调用语句
  9. Android应用性能优化方案
  10. sql server 权限
  11. Oracle12c:支持通过创建identity columen来实现创建自增列
  12. 路由网关---zuul
  13. file 文件上传后缀转化小写
  14. 红黑树与AVL
  15. vue框架搭建
  16. mac下查看jdk安装版本及安装目录
  17. Cocos2d 编译js为jsc bytecode文件
  18. Vue2学习笔记:实例生命周期
  19. poj 3294 后缀数组 多字符串中不小于 k 个字符串中的最长子串
  20. 如何在 Debian 9 下安装 LEMP 和 WHMCS 7.5

热门文章

  1. electron api sendInputEvent 源码
  2. STM32WB AHB总线、APB总线与外设
  3. win10电脑配置
  4. Android异常与性能优化相关面试问题-ANR异常面试问题详解
  5. 【安徽集训】Entropy
  6. HttpServletResponse 返回的json数据不是json字符串,而是json对象
  7. Tomcat 调优测试
  8. updatedepthtexture 和 screen space shadow 开关
  9. AMD 异步模块定义
  10. Python字符串拼接的五种方式