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. Graveyard(poj3154)

    Graveyard Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 1289   Accepted: 660   Specia ...

  2. 封装7z软件实现批量文件或目录压缩

    哈哈,作为一个特别懒的运维人来说 兄弟我写了一个批量压缩文件或目录的小工具,用来批量压缩文件目录 弄一下,然后就不用管他了,后天看结果就好了 操作步骤: 1.选择想做压缩处理的根目录 2.选择你要的功 ...

  3. Flask中request参数

    首先要明确一件事,Request这是个对象,不管使用PHP还是python还是什么java语言,虽然request这个对象可能叫的名字不一样,(在其他语言中可能叫什么HttpRequest),但是原理 ...

  4. JS 模块 p6

    利用了闭包的模块: 简单模块例子: function fn(){ ; function y(){ console.log(x); } return { y:y} }var do1 = fn() do1 ...

  5. 撩课-Web大前端每天5道面试题-Day1

    1. var的变量提升的底层原理是什么? JS引擎的工作方式是: 1) 先解析代码,获取所有被声明的变量: 2)然后在运行.也就是说分为预处理和执行两个阶段. 变量提升:所有变量的声明语句都会被提升到 ...

  6. Mariadb MySQL逻辑条件判断相关语句、函数使用举例介绍

    MySQL逻辑条件判断相关语句.函数使用举例介绍 By:授客 QQ:1033553122 1.  IFNULL函数介绍 IFNULL(expr1, expr2) 说明:假如expr1 不为NULL,则 ...

  7. android-studio开发NDK错误记录:bash: ../../build/intermediates/classes/debug: is a directory

    按照网上很多已有的教程,在用javah生成c的头文件时候报错: Error: no classes specified bash: ../../build/intermediates/classes/ ...

  8. Java学习笔记之异常处理

    一.异常的分类 1.由Java虚拟机抛出的异常(Error):程序无法处理的问题,用户不用去进行处理(虚拟机错误丶内存溢出错误丶线程死锁) 2.Exception异常:程序本身可以进行处理的异常 1. ...

  9. Hive 锁 lock

    Hive + zookeeper 可以支持锁功能 锁有两种:共享锁.独占锁,Hive开启并发功能的时候自动开启锁功能 1)查询操作使用共享锁,共享锁是可以多重.并发使用的 2)修改表操作使用独占锁,它 ...

  10. Android Monkey的使用

    转载请标明出处:http://blog.csdn.net/zhaoyanjun6/article/details/71750907 本文出自[赵彦军的博客] 什么是 Monkey Monkey 是一个 ...