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. javascript对下拉列表框(select)的操作

    <form id="f"> <select size="1" name="s"> <option value= ...

  2. 【BZOJ】3391: [Usaco2004 Dec]Tree Cutting网络破坏(dfs)

    http://www.lydsy.com/JudgeOnline/problem.php?id=3391 显然判断每个点只需要判断子树是否小于等于n/2即可 那么我们虚拟一个根,然后计算每个子树的si ...

  3. hdu 5360 Hiking(优先队列+贪心)

    题目:http://acm.hdu.edu.cn/showproblem.php? pid=5360 题意:beta有n个朋友,beta要邀请他的朋友go hiking,已知每一个朋友的理想人数[L, ...

  4. php中数组中&的问题

    1.代码: <?php $arr = array('one','two','three'); foreach ($arr as $value){ echo 'Value:'.$value.'&l ...

  5. C# Expression 树转化为SQL语句(一)

    sql有有四中基本语句,分别是增删改查,在建立model后如何生成这四中sql语句,降低开发时间. 我们先模拟出一张学生表: public class Student { public int id ...

  6. python中函数参数*args和**kw的区别

    1.函数与参数(实参) 在python中创建函数是def,创建函数名是def f(),f函数名字,def f(a,b),这里的a,b是两个参数,函数名是自定义的,参数也是自定义,随意就好.看图如下效果 ...

  7. c++ 类声明

    class B; struct A { B* ptr; }; class B { public: }; int main() { ; } A中定义了B的指针,所以要声明class B,在定义处于不完整 ...

  8. 【BZOJ4773】负环 倍增Floyd

    [BZOJ4773]负环 Description 在忘记考虑负环之后,黎瑟的算法又出错了.对于边带权的有向图 G = (V, E),请找出一个点数最小的环,使得 环上的边权和为负数.保证图中不包含重边 ...

  9. java如何计算两个经纬度之间的距离?

    /*计算两个经纬度之间的距离 结果单位:米 */public static double getDistance(String lat1Str, String lng1Str, String lat2 ...

  10. CodeForces 666B World Tour(spfa+枚举)

    B. World Tour time limit per test 5 seconds memory limit per test 512 megabytes input standard input ...