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

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. linux工具之iostat

    iostat 是I/O  statistics(输入/输出统计)缩写iostat工具将对系统磁磁盘活动进行监视iostat属于sysstat软件包可以用yum   install   sysstat ...

  2. python文件操作:文件处理案例

    储存一个文件,文件上有多个用户名,密码,做一个认证的流程程序,首先创建一个文件,文件上输入多个用户名,及对应的密码,然后让客户输入用户名和密码,进行用户名和密码核对,如果输入正确,则的认证成功,bre ...

  3. MYSQL获得查询记录的行号

    对于获得查询记录的行号这一功能,Oracle 中可以使用标准方法(8i版本以上),也可以使用非标准的ROWNUM,MS SQL Server 则在 2005 版本中提供了ROW_NUMBER()函数. ...

  4. 【转】关于 Ruby 解释器:一些你需要知道的

    关于 Ruby 解释器:一些你需要知道的 原文:Ruby Interpreters: What You Need to Know 使用正确的 Ruby 解释器来运行程序可以发挥重要作用,不幸的是很难找 ...

  5. 电脑同时安装了python2和python3后,随意切换版本并使用pip安装

    第一步: python2安装路径下python.exe重命名为python2.exe,python3安装路径下python.exe重命名为python3.exe; 第二步: 分别为python2.ex ...

  6. Winforms界面开发DevExpress v19.2:Map、Pivot Grid等功能增强

    DevExpress Winforms Controls 内置140多个UI控件和库,完美构建流畅.美观且易于使用的应用程序.无论是Office风格的界面,还是分析处理大批量的业务数据,DevExpr ...

  7. uniapp上传图片转base64码

    uni.chooseImage({ count: 9, success: res => { this.imageList = this.imageList.concat(res.tempFile ...

  8. JavaScript算术运算符

    ㈠运算符(操作符) ⑴通过运算符可以对一个或多个值进行运算,并获取运算结果 ⑵比如:typeof就是运算符,可以来获得一个值得类型               它会将该值的类型以字符串的形式返回   ...

  9. RAID技术超详细讲解

    RAID 技术是一种多磁盘技术,面对数据的各方面有着两面性的影响,整体来说优点大于缺点的,下面我将详细介绍一下 RAID ,简称磁盘阵列技术. 一.RAID 概述 1988 年美国加州大学伯克利分校的 ...

  10. java中的排列组合

    使用之前需要声明一个Combine的对象,调用startCombile方法,可返回想要的组合数或者个数,参数介绍很重要 public class Combine { private Object[] ...