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:

  1. Input: "()"
  2. Output: 1

Example 2:

  1. Input: "(())"
  2. Output: 2

Example 3:

  1. Input: "()()"
  2. Output: 2

Example 4:

  1. Input: "(()(()))"
  2. 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 是表示深度的一个概念。

  1. class Solution {
  2. public:
  3. int scoreOfParentheses(string S) {
  4. int ret = , cnt = ;
  5. char last = ' ';
  6. for(auto ch : S){
  7. if(ch == '('){
  8. cnt++;
  9. }else {
  10. cnt--;
  11. if(last == '('){
  12. ret += (<<cnt);
  13. }
  14. }
  15. last = ch;
  16. }
  17. return ret;
  18. }
  19. };

LC 856. Score of Parentheses的更多相关文章

  1. Leetcode 856. Score of Parentheses 括号得分(栈)

    Leetcode 856. Score of Parentheses 括号得分(栈) 题目描述 字符串S包含平衡的括号(即左右必定匹配),使用下面的规则计算得分 () 得1分 AB 得A+B的分,比如 ...

  2. 856. Score of Parentheses

    Given a balanced parentheses string S, compute the score of the string based on the following rule: ...

  3. LeetCode 856. Score of Parentheses 括号的分数

    其实是这道题的变式(某港带同学的C/C++作业) 增加一点难度,输入的S不一定为平衡的,需要自己判断是否平衡,若不平衡输出为0. 题目描述 Given a parentheses string s, ...

  4. 【LeetCode】856. Score of Parentheses 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 栈 递归 计数 日期 题目地址:https://le ...

  5. LeetCode 856. 括号的分数(Score of Parentheses)

    856. 括号的分数 856. Score of Parentheses 题目描述 给定一个平衡括号字符串 S,按下述规则计算该字符串的分数: () 得 1 分. AB 得 A + B 分,其中 A ...

  6. [LeetCode] Score of Parentheses 括号的分数

    Given a balanced parentheses string S, compute the score of the string based on the following rule: ...

  7. [Swift]LeetCode856. 括号的分数 | Score of Parentheses

    Given a balanced parentheses string S, compute the score of the string based on the following rule: ...

  8. (栈)leetcode856 Score of Parentheses

    Given a balanced parentheses string S, compute the score of the string based on the following rule: ...

  9. leetcode-856 Score of Parentheses

    Given a balanced parentheses string S, compute the score of the string based on the following rule: ...

随机推荐

  1. web项目部署在centos 7验证码显示不出来解决方案

    今天把项目部署在centos7上,发现验证码显示不出来,看了一下tomcat日志 Exception in thread "http-nio-8080-exec-3" java.l ...

  2. Delphi MSComm控件事件的介绍

  3. NETGEAR 系列路由器命令执行漏洞简析

    NETGEAR 系列路由器命令执行漏洞简析 2016年12月7日,国外网站exploit-db上爆出一个关于NETGEAR R7000路由器的命令注入漏洞.一时间,各路人马开始忙碌起来.厂商忙于声明和 ...

  4. 网络协议相关面试问题-DNS相关面试问题

    对于网络上的大部通讯都是基于TCP/IP协议的, 其中最重要的是IP协议,它是基于IP地址的,而计算机通讯只能识别IP地址,如192.168.0.1,而不能识别像咱们在浏览器敲得见名之义的" ...

  5. 第六章 组件 56 组件-组件中的data

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8&quo ...

  6. 用github page为自己的项目编写文档

    ———————————————— 一.用md文件建立网页这一步要做的就是把四个实验报告做出来.1.首先为了方便管理,我们在ML下创建一个名为docs的文件夹. 2.然后在修改设置,让github从do ...

  7. Java-20180412

    今天开始重新复习Java,完成了leetcode的第一题. 1.算法: 给定一个数组和目标值,找出相加等于目标值的数组元素的下标. 数组[2,7,11,15]; target:9; 返回:[0,1]; ...

  8. gtid同步异常处理

    gtid同步异常处理 分析出现问题时候GTID值 通过分析法获取gtid值 通过查看mysql> show slave status \G;查看一下信息并记录下来:Retrieved_Gtid_ ...

  9. hive2.3.4安装

    一.安装Hadoop Hive运行在Hadoop环境之上,因此需要hadoop环境,本次在安装在hadoop完全分布式模式的namennode节点上 请参考:hadoop搭建 二.安装Hive 下载 ...

  10. IDEA运行有问题debug正常解决方案

    朋友们!有没有遇到这样的问题,IDEA运行有问题,debug确是正常的,不经怀疑人生! 不要慌!点击maven,clean一下,再compile一下,就好啦! 不要慌!点击maven,clean一下, ...