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 括号得分(栈)的更多相关文章

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

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

  2. LC 856. Score of Parentheses

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

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

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

  4. 【LeetCode】856. Score of Parentheses 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 栈 递归 计数 日期 题目地址:https://le ...

  5. 【LeetCode】22. Generate Parentheses 括号生成

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:括号, 括号生成,题解,leetcode, 力扣,Pyt ...

  6. 856. Score of Parentheses

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

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

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

  8. 【LeetCode】20. Valid Parentheses 有效的括号

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:有效,括号,括号匹配,栈,题解,leetcode, 力扣 ...

  9. [LeetCode] 032. Longest Valid Parentheses (Hard) (C++)

    指数:[LeetCode] Leetcode 指标解释 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 032. Lon ...

随机推荐

  1. Linux Virtualization with Xen

    Xen is the new virtualization kid on the block. It's gaining visibility and importance at a speed on ...

  2. 5-SOM神经网络

    https://blog.csdn.net/gavinlyy/article/details/8302814 https://blog.csdn.net/u011314012/article/deta ...

  3. [C++] right value reference

    the advantage of  right value reference: Memory Optimization save memory copy

  4. mysqli_query(“set nams utf8”)为FALSE

    经过过测试: mysqli_query("set nams  utf8")必须在所有语句的执行之前,否则会返回FALSE. 看手册,说不建议用mysqli_query来设置编码,用 ...

  5. easyui tabs update 强制刷新页面

    var tab = artDialog.open.origin.$("#tabs").tabs("getTab", "公司评级"); var ...

  6. Part2_lesson2---ARM处理器工作模式

    arm公司发布的学习手册:ARM Architecture Reference Manual. 打开之: 找到Programmers' Model->A2.2 Processor modes. ...

  7. CMDB小练习

    为什么要用CMDB? 因为公司之前统计资产信息用的是excel表格,随着业务的增加和信息的变更,这个表格变得越来越乱,所以我们就想着编写出一套自动管理资产信息的系统,实现自动管理资产信息 三种方案? ...

  8. Qt之生成pdf(转)

    Qt中如何让图片.文本.HTML或者其他形式的内容生成pdf呢?主要利用QPrinter来实现,QPrinter不止可以操作打印机来打印纸张文件,并且可以将文件保存至磁盘,存储为pdf格式的文件.   ...

  9. 两款JSON类库Jackson与JSON-lib的性能对比(新增第三款测试)

    本篇文章主要介绍了"两款JSON类库Jackson与JSON-lib的性能对比(新增第三款测试)",主要涉及到两款JSON类库Jackson与JSON-lib的性能对比(新增第三款 ...

  10. oracle中的表空间(tablespace)、方案(schema)、段(segment)、区(extent)、块(block)

    数据文件和日志文件是数据库中最重要的文件.它们是数据存储的地方.每个数据库至少有一个与之相关的数据文件,通常情况下不只一个,有很多.数据在数据文件中是如何组织的?要了解这些内容我们首先必须理解什么是表 ...