LeetCode:括号的分数【856】

题目描述

给定一个平衡括号字符串 S,按下述规则计算该字符串的分数:

  • () 得 1 分。
  • AB 得 A + B 分,其中 A 和 B 是平衡括号字符串。
  • (A) 得 2 * A 分,其中 A 是平衡括号字符串。

示例 1:

输入: "()"
输出: 1

示例 2:

输入: "(())"
输出: 2

示例 3:

输入: "()()"
输出: 2

示例 4:

输入: "(()(()))"
输出: 6

提示:

  1. S 是平衡括号字符串,且只含有 ( 和 ) 。
  2. 2 <= S.length <= 50

题目分析

我们可以使用递归来解决这个问题。通过分析题目,我们发现有三种情况:

比如:

SCORE("()(()())")

  =SCORE("()")+SCORE("(()())")

  =1+2*SCORE("()()")

  =1+2*(SCORE("()")+SCORE("()"))

  =1+2*2

  =5

说明:

我们在代码中怎么来识别,(()())这种情况呢,如果从最左边到倒数第二位是不平衡的,那么加上最后一位一定是平衡的,这也说明,第一位和最后一位是一个大括号,那当然是CASE1这种情况。

Java题解

class Solution {
public int scoreOfParentheses(String S) {
return score(S,0,S.length()-1);
} public int score(String S,int left,int right)
{
if(right-left==1)
return 1;
int b = 0;
for(int i=left;i<right;i++)
{
if(S.charAt(i)=='(')
b++;
if(S.charAt(i)==')')
b--;
if(b==0)
return score(S,left,i)+score(S,i+1,right);
}
return 2*score(S,left+1,right-1);
}
}

LeetCode:括号的分数【856】的更多相关文章

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

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

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

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

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

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

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

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

  5. [LeetCode] Rank Scores 分数排行

    Write a SQL query to rank scores. If there is a tie between two scores, both should have the same ra ...

  6. LeetCode:178.分数排名

    题目链接:https://leetcode-cn.com/problems/rank-scores/ 题目 编写一个 SQL 查询来实现分数排名.如果两个分数相同,则两个分数排名(Rank)相同.请注 ...

  7. leetcode - 括号字符串是否有效

    括号字符串是否有效 给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效. 有效字符串需满足: 左括号必须用相同类型的右括号闭合. 左括号必须以正确的顺序闭合. ...

  8. leetcode 括号

    1. 括号(0809) 设计一种算法,打印n对括号的所有合法的(例如,开闭一一对应)组合. 说明:解集不能包含重复的子集. 例如,给出 n = 3,生成结果为: [ "((()))" ...

  9. Swift LeetCode 目录 | Catalog

    请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift    说明:题目中含有$符号则为付费题目. 如 ...

随机推荐

  1. hdu 3836 Equivalent Sets(强连通分量--加边)

    Equivalent Sets Time Limit: 12000/4000 MS (Java/Others)    Memory Limit: 104857/104857 K (Java/Other ...

  2. python 的简单抓取图片

    在我们日常上网浏览网页的时候,经常会看到一些好看的图片,我们就希望把这些图片保存下载,或者用户用来做桌面壁纸,或者用来做设计的素材. 我们最常规的做法就是通过鼠标右键,选择另存为.但有些图片鼠标右键的 ...

  3. 数据库I/O:CMP、Hibernate

    ★什么是“Persistence” 用过VMWare的朋友大概都知道当一个guest OS正在运行的时候点击“Suspend”将虚拟OS挂起,它会把整个虚拟内存的内容保存到磁盘上,譬如你为虚拟OS分配 ...

  4. Codeforces Round #268 (Div. 2) (被屠记)

    c被fst了................ 然后掉到600+.... 然后...估计得绿名了.. sad A.I Wanna Be the Guy 题意:让你判断1-n个数哪个数没有出现.. sb题 ...

  5. centos6.5安装apache2

    参考 http://my.oschina.net/u/593517/blog/340289 http://blog.csdn.net/hkmaike/article/details/9732177

  6. 【Cloud Foundry】Cloud Foundry学习(四)——Service

    在阅读的过程中有不论什么问题,欢迎一起交流 邮箱:1494713801@qq.com    QQ:1494713801 Services:Cloud Foundry的Service模块从源码控制上看就 ...

  7. opencv2.4.9+VS2010配置

    opencv2.4.9 https://pan.baidu.com/s/15b5bEY65R4CptayEYVAG4A 安装包路径:D:\文件及下载相关\文档\Tencent Files\845235 ...

  8. c/c++中内存对齐完全理解

    一,什么是内存对齐?内存对齐用来做什么? 所谓内存对齐,是为了让内存存取更有效率而采用的一种编译阶段优化内存存取的手段. 比如对于int x;(这里假设sizeof(int)==4),因为cpu对内存 ...

  9. c++调用python函数时,使用PyArray_SimpleNewFromData(nd, dims, typenum, data)函数时出现内存错误的问题

    示例程序: int main(int argc, char *argv[]){ PyObject *pName, *pModule, *pDict, *pFunc, *pValue, *pArgs,* ...

  10. boost::archive::binary_iarchive

    #include <iostream> #include <string> #include <sstream> #include <vector> # ...