LeetCode:括号的分数【856】
LeetCode:括号的分数【856】
题目描述
给定一个平衡括号字符串 S,按下述规则计算该字符串的分数:
()得 1 分。AB得A + B分,其中 A 和 B 是平衡括号字符串。(A)得2 * A分,其中 A 是平衡括号字符串。
示例 1:
输入: "()"
输出: 1
示例 2:
输入: "(())"
输出: 2
示例 3:
输入: "()()"
输出: 2
示例 4:
输入: "(()(()))"
输出: 6
提示:
S是平衡括号字符串,且只含有(和)。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】的更多相关文章
- LeetCode 856. 括号的分数(Score of Parentheses)
856. 括号的分数 856. Score of Parentheses 题目描述 给定一个平衡括号字符串 S,按下述规则计算该字符串的分数: () 得 1 分. AB 得 A + B 分,其中 A ...
- LeetCode 856. Score of Parentheses 括号的分数
其实是这道题的变式(某港带同学的C/C++作业) 增加一点难度,输入的S不一定为平衡的,需要自己判断是否平衡,若不平衡输出为0. 题目描述 Given a parentheses string s, ...
- [LeetCode] Score of Parentheses 括号的分数
Given a balanced parentheses string S, compute the score of the string based on the following rule: ...
- [Swift]LeetCode856. 括号的分数 | Score of Parentheses
Given a balanced parentheses string S, compute the score of the string based on the following rule: ...
- [LeetCode] Rank Scores 分数排行
Write a SQL query to rank scores. If there is a tie between two scores, both should have the same ra ...
- LeetCode:178.分数排名
题目链接:https://leetcode-cn.com/problems/rank-scores/ 题目 编写一个 SQL 查询来实现分数排名.如果两个分数相同,则两个分数排名(Rank)相同.请注 ...
- leetcode - 括号字符串是否有效
括号字符串是否有效 给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效. 有效字符串需满足: 左括号必须用相同类型的右括号闭合. 左括号必须以正确的顺序闭合. ...
- leetcode 括号
1. 括号(0809) 设计一种算法,打印n对括号的所有合法的(例如,开闭一一对应)组合. 说明:解集不能包含重复的子集. 例如,给出 n = 3,生成结果为: [ "((()))" ...
- Swift LeetCode 目录 | Catalog
请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift 说明:题目中含有$符号则为付费题目. 如 ...
随机推荐
- 什么是AOP和OOP,IOC和DI有什么不同?
什么是AOP和OOP,IOC和DI有什么不同? 解答: 1)面向对象编程(Object Oriented Programming,OOP,面向对象程序设计)是一种计算机编程架构.AOP是OOP的延续, ...
- 消息队列ipc的一些设置
Linux IPC 参数设定- 命令方式: echo 80 > /proc/sys/vm/overcommit_ratio, etc MSGMNB 每个消息队列的最大字节限制. MSGMNI 整 ...
- Stanford CoreNLP 3.6.0 中文指代消解模块调用失败的解决方案
当前中文指代消解领域比较活跃的研究者是Chen和Vincent Ng,这两个人近两年在AAAI2014, 2015发了一些相关的文章,研究领域跨越零指代.代词指代.名词指代等,方法也不是很复杂,集中于 ...
- 使用Navicat for MySQL
1.打开Navicat for MySQL 2.新建连接 3.新建数据库 4.在新建的数据库上运行SQL文件
- Hibernate中持久化类与持久化对象
1.JavaBean类 JavaBean类是实体类,必须一下属性,private修饰的成员属性,public修饰的getter与setter访问方法,public修饰的空参构造器,实现Serializ ...
- 语法之ADO.NET
ADO.NET的概念 由于本系列并不是主讲ADO.NET.所以这里笔者只会教上面定义有线连接方式相关的类.不管如何让我们先看一下ADO.NET类相关联的所有基类吧.这样子也方便我们下面的学习. 下面是 ...
- PAT Advance 1014
题目: 1014. Waiting in Line (30) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue S ...
- Android实例-退出程序
Android实例-退出程序 http://www.cnblogs.com/FKdelphi unit Unit1; interface uses System.SysUtils, System.Ty ...
- git GUI 入门
一:安装一个git 及gui 二:配置gui及线上的git链接 在Git Gui中,选择Remote->add添加远程服务器,远程服务器信息有两种填写方式,填写https地址或ssh地址,对应g ...
- jQuery Validation Engine 表单验证,自定义规则验证方法
jQuery Validation Engine 表单验证说明文档http://code.ciaoca.com/jquery/validation-engine/ js加到jquery.validat ...