Leetcode 856. Score of Parentheses 括号得分(栈)
Leetcode 856. Score of Parentheses 括号得分(栈)
题目描述
字符串S包含平衡的括号(即左右必定匹配),使用下面的规则计算得分
()
得1分AB
得A+B的分,比如()()
得2分(A)
得2A分, 比如(()())
得2(1+1)分
测试样例
Example 1:
Input: "()"
Output: 1
Example 2:
Input: "(())"
Output: 2
Example 3:
Input: "()()"
Output: 2
Example 4:
Input: "(()(()))"
Output: 6
详细分析
简而言之,遇到右括号就一直出栈并累加到一个值直到遇到左括号,这个累加值就表示这对括号的得分。如此周而复始到字符串结尾即可。
具体言之,遇到 ( 就入栈,这里入一个毒药值-1,然后遇到 ) 就一直出栈,累加到score,直到遇到毒药值-1,最后把累加的score入栈,表示这对括号的最终得分。
最后计算栈中的结果之和即可。至于为什么是栈结果和而不是栈顶一个值是因为输入可能是()()
,这种,最后栈中是| 1 | 1 |
。
算法实现
class Solution {
public:
int scoreOfParentheses(string S) {
stack<int> s;
int i=0;
while(S[i]!=0){
if(S[i]=='('){
s.push(-1);
}else if(S[i]==')'){
int score = 0;
if(s.top()==-1){
s.pop();
s.push(1);
}else{
while(!s.empty()){
int t = s.top();
if(t==-1){
s.pop();
s.push(score);
break;
}
score+= t*2;
s.pop();
}
}
}
i++;
}
int result = 0;
while(!s.empty()){
result += s.top();
s.pop();
}
return result;
}
};
Leetcode 856. Score of Parentheses 括号得分(栈)的更多相关文章
- LeetCode 856. Score of Parentheses 括号的分数
其实是这道题的变式(某港带同学的C/C++作业) 增加一点难度,输入的S不一定为平衡的,需要自己判断是否平衡,若不平衡输出为0. 题目描述 Given a parentheses string s, ...
- LC 856. Score of Parentheses
Given a balanced parentheses string S, compute the score of the string based on the following rule: ...
- [LeetCode] Score of Parentheses 括号的分数
Given a balanced parentheses string S, compute the score of the string based on the following rule: ...
- 【LeetCode】856. Score of Parentheses 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 栈 递归 计数 日期 题目地址:https://le ...
- 【LeetCode】22. Generate Parentheses 括号生成
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:括号, 括号生成,题解,leetcode, 力扣,Pyt ...
- 856. Score of Parentheses
Given a balanced parentheses string S, compute the score of the string based on the following rule: ...
- LeetCode 856. 括号的分数(Score of Parentheses)
856. 括号的分数 856. Score of Parentheses 题目描述 给定一个平衡括号字符串 S,按下述规则计算该字符串的分数: () 得 1 分. AB 得 A + B 分,其中 A ...
- 【LeetCode】20. Valid Parentheses 有效的括号
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:有效,括号,括号匹配,栈,题解,leetcode, 力扣 ...
- [LeetCode] 032. Longest Valid Parentheses (Hard) (C++)
指数:[LeetCode] Leetcode 指标解释 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 032. Lon ...
随机推荐
- Maven面试宝典啊
一.Maven有哪些优点和缺点 优点如下: 简化了项目构建.依赖管理: 易于上手,对于新手可能一个"mvn clean package"命令就可能满足他的工作 便于与持续集成工具( ...
- IPP库下FFT的基本实现
首先感谢韩昊同学,他的傅里叶分析入门给我们对数学公式不熟悉的人了解傅里叶算法打开了一扇窗户,其原文发表于知乎:https://zhuanlan.zhihu.com/p/19763358 在了解其基本原 ...
- 对max_allowed_packet这个参数的误解
之前一篇文章知识对,这个参数一个泛泛的概念,但是并没有理解这个参数的真正意义,现在差不多有那么点儿感觉了,主要的意思是每一条记录是一个包,不可拆分,而且包括blob,text等大字段.
- scrapy定制爬虫-爬取javascript——乾颐堂
很多网站都使用javascript...网页内容由js动态生成,一些js事件触发的页面内容变化,链接打开.甚至有些网站在没有js的情况下根本不工作,取而代之返回你一条类似"请打开浏览器js& ...
- stristr函数
- yii2项目实战-访问控制过滤器ACF讲解
作者:白狼 出处:http://www.manks.top/document/yii2-filter-control.html 本文版权归作者,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明 ...
- 编写高质量代码改善C#程序的157个建议——建议118:使用SecureString保存密钥等机密字符串
建议118:使用SecureString保存密钥等机密字符串 托管代码中的字符串是一类特殊的对象,它们不可用被改变.每次使用System.String类张的方法之一时,或者使用此类型进行运算时(如赋值 ...
- Linux CentOS 7 安装confluence 5.8.10
一..需要下载的安装包如下 1.atlassian-confluence-5.8.10-x64.bin #confluence安装包 2.confluence5.x-crack.zip #conflu ...
- jeecms栏目模型和内容模型的使用以及对应前台的标签中的属性名
第一步:模型管理-添加模型: 栏目模板前缀设定方案下的channel目录下的ch_menu.html作为浏览栏目的模板页.对应访问网址:项目名/栏目名(一级或者二级栏目如news或者gnxw)/ind ...
- mysql数据库使用sql查询数据库大小及表大小
网上查了很多资料,最后发现一个可行的,分享如下: 数据库大小查询: select concat(round(sum(DATA_LENGTH/1024/1024),2),'M') from inform ...