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. 深入理解mysql的隔离级别

    建表插入测试数据A> create table test(id int ,num int) ;Query OK, 0 rows affected (0.53 sec) A> insert ...

  2. LINK : fatal error LNK1104: cannot open file "mfc42d.lib"

    VC++6.0上建立了个基于MFC应用程序,在编译时候没出现错误,但在LINK的是时候出现这样的错误:Linking...LINK : fatal error LNK1104: cannot open ...

  3. Android TV上的焦点切换效果

    转载:http://blog.csdn.net/wzlas111/article/details/39741091 Android TV上的焦点凸显特效相信大家都看到过,那么我们就来实现它吧,首先上张 ...

  4. NAT穿透的详细讲解及分析.RP

    原创出处:https://bbs.pediy.com/thread-131961.htm 转载来源: https://blog.csdn.net/g_brightboy/article/details ...

  5. Perl 学习笔记-哈希

    1.Perl中的哈希 高效快捷, 没有大小限制. 大哈希一样很快! 命名: 和Perl其他标识符一样, 同时拥有自己的的名字空间.  $roger{"sex"} 和 $roger没 ...

  6. Mysql简介与编译安装

    ==========MYSQL工作原理图: 1>数据库简介:简单的说数据库(database)就是一个存储数据的仓库,它将数据按照特定的规律存储到磁盘上,通过数据库管理系统,能够有效的管理存储在 ...

  7. 18、Semantic-UI之进度条

      在使用进度条的时候也是必须要结合项目中ajax和后台数据结合使用的. 示例:定义基础进度条 <div class="ui progress"> <div cl ...

  8. 如何让X5发现你的手机

    1. 手机开启 USB 调试.不用 ROOT.2. 装驱动.(问题就在这里) 首先要装对驱动,如果你的驱动叫 MyHTC,请立即删除. 找个 手机助手.例如 百度 腾讯 360 等等.反正不论你是谁的 ...

  9. 系统数据库--修改tempdb的位置

    use mastergoAlter database tempdb modify file (name = tempdev, filename = 'G:\db\tempdb.mdf')goAlter ...

  10. 解决Struts2拦截器的对于参数传递无效问题

    今天做项目时,使用拦截器对用户权限检查.拦截器本身没有问题,可是实现权限拦截,但是传递的参数全部都无效了.搞了很久,由于对拦截器的内部机制不是特别熟悉,所以重新研读了一下Struts2的拦截器.找到了 ...