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 说明:题目中含有$符号则为付费题目. 如 ...
随机推荐
- vmware复制虚拟机出现Error:No suitable device found:no device found for connection 'System eth0'
vmware复制虚拟机出现Error:No suitable device found:no device found for connection 'System eth0' 废话不多说,直接给出解 ...
- Windwos下连远程linux Hbase小问题
前几天,兴起想仔细玩玩hbase,细细去研究一下,写了个小demo,从win7去连接另一台T510的ubuntu上的hbase.很简单的crud的操作程序,没有看出来什么问题,但是跑起来,硬是好像bl ...
- kafka1:Kafka集群部署步骤
参考: kafka 集群--3个broker 3个zookeeper创建实战 细细品味Kafka_Kafka简介及安装_V1.3http://www.docin.com/p-1291437890.ht ...
- python 的时间复杂度
Python内置方法的时间复杂度 本文翻译自Python Wiki 本文基于GPL v2协议,转载请保留此协议. 本页面涵盖了Python中若干方法的时间复杂度(或者叫“大欧”,“Big O”).该时 ...
- Android UI开发第三十九篇——Tab界面实现汇总及比较
Tab布局是iOS的经典布局,Android应用中也有大量应用,前面也写过Android中TAb的实现,<Android UI开发第十八篇——ActivityGroup实现tab功能>.这 ...
- 【BZOJ3166】[Heoi2013]Alo 可持久化Trie树+set
[BZOJ3166][Heoi2013]Alo Description Welcome to ALO ( Arithmetic and Logistic Online).这是一个VR MMORPG , ...
- iOS 程序切换后台
1. -(void)animationFinished:(NSString*)animationid finished:(NSNumber*)finished context:(void*)conte ...
- @RequestMapping中method的默认值是什么?
@RequestMapping中method的默认值是什么? 没有默认值,如果不配置method, 则以任何请求形式 RequestMethod.GET, RequestMethod.POST, Re ...
- etcd集群安装部署
1. 集群架构 由于我们只有两个机房,所以选择的是以上图中所示的数据同步方案, 通过做镜像的方式保证两个集群的数据实时同步. 整体架构如上图所示, 整个全局元数据中心包括两套集群,廊坊集群和马驹桥集群 ...
- Powershell Get-registerkey(susid)
$servers=get-content D:\serverregister.txt Get-registerkey -ComputerName $servers | select computer, ...