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

想法:设立三个变量,一个变量result代表最终的运算结果,初始化为0,。一个变量left代表左括号的个数,并且初始为1。一个变量right代表有括号的个数,初始为0。如何是Example 1 和Example 3这种情况,直接统计匹配的个数即可。对于Example 2这种情况,其数学含义表示为2的次幂,此时只需统计左后括号的差值。

class Solution {
public:
    int scoreOfParentheses(string S) {
        ;
        ;
        ;
        int len = S.size();
         == len)
            return result;
         ; i <= len ; i++){
            if(S[i] == '('){
                left++;
            }else{
                right++;
                ] == '('){
                    result+=pow(,left-right);
                }
            }
        }
        return result;
    }
};

leetcode-856 Score of Parentheses的更多相关文章

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

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

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

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

  3. LC 856. Score of Parentheses

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

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

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

  5. 856. Score of Parentheses

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

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

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

  7. [LeetCode] 032. Longest Valid Parentheses (Hard) (C++)

    指数:[LeetCode] Leetcode 指标解释 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 032. Lon ...

  8. 【一天一道LeetCode】#22. Generate Parentheses

    一天一道LeetCode (一)题目 Given n pairs of parentheses, write a function to generate all combinations of we ...

  9. 乘风破浪:LeetCode真题_022_Generate Parentheses

    乘风破浪:LeetCode真题_022_Generate Parentheses 一.前言 关于括号的题目,我们已经遇到过了验证正确性的题目,现在让我们生成合法的括号列表,怎么办呢?想来想去还是递归比 ...

  10. 乘风破浪:LeetCode真题_020_Valid Parentheses

    乘风破浪:LeetCode真题_020_Valid Parentheses 一.前言 下面开始堆栈方面的问题了,堆栈的操作基本上有压栈,出栈,判断栈空等等,虽然很简单,但是非常有意义. 二.Valid ...

随机推荐

  1. Android - fragment Manager

    fragment基本使用: http://www.cnblogs.com/qlky/p/5415679.html Fragmeng优点 Fragment可以使你能够将activity分离成多个可重用的 ...

  2. SpringBoot启动加载类ApplicationRunner

    SpringBoot启动加载类ApplicationRunner 有时希望项目在启动的时候加载一些系统参数,就要用到ApplicationRunner ApplicationRunner是一个接口,我 ...

  3. 悟空模式-java-原型模式

    [却说那妖精与大圣斗经半日,不分胜败.行者把棒丢起,叫一声“变!”就以一变十,以十变百,以百变千,半天里,好似蛇游蟒搅,乱打妖邪.妖邪慌了手脚,将身一闪,化道清风,即奔碧空之上逃走.行者念声咒语,将铁 ...

  4. jQuery中.bind() .live() .delegate() .on()区别

    $(selector).bind(event,data,function) $(selector).live(event,data,function)//jquery1.9版本以下支持,jquery1 ...

  5. 只有mdf文件的恢复方法

    EXEC sp_attach_single_file_db @dbname = 'AdventureWorksDW2012_Data',@physname = 'D:\Program Files (x ...

  6. 学习笔记(5)——实验室集群LVS监控Web界面配置

    症状:通过虚拟IP能访问到监控页面:http://192.168.253.110/ipvsadm.php,但是却无法读出LVS任务分发及集群负载信息. 打开ipvsadm.php页面,源码如下: &l ...

  7. session与cookie的区别和用法

    一.session 1.保存在服务器的,每个人存一份2.可以存储任何类型数据3.有一个默认过期时间注意:在所有使用session的页面最顶端要开启session---session_start();存 ...

  8. MonkeyRunner进坑——errors importing other modules

    后知后觉,刚知道Android提供了MonkeyRunner这么个东西,能用来干嘛呢,官方文档介绍得很清楚.简单说,可以用Python程序通过API, installs an Android appl ...

  9. Android热修复之 - 收集崩溃信息上传服务器

    1.概述 大致的流程就是在用户崩溃的时候,我们获取崩溃信息.应用当前的信息和手机信息,然后把它保存到手机内存卡,再找我就直接找出来看看.后来衍生到上线后某些奇葩机型会有部分问题,所以不得不上传到服务器 ...

  10. Unity Profiler连接Android真机调试

    Profiler在Editor模式就可以观看性能消耗,但是毕竟电脑配置高,跟手机真机环境还是有区别.实际开发中的优化还是推荐用真机测试. 因为IOS一般比Android手机的配置高,在Android平 ...