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. H5/

    1.value: 2.selected="selected": 设置selected="selected"属性,则该选项就被默认选中. 下拉列表也可以进行多选操 ...

  2. CloudStack + KVM + HA

    KVM高可用性CS4.2暂时没有实现 The Linux Kernel Virtual Machine (KVM) is a very popular hypervisor choice amongs ...

  3. hive1.2.1问题集锦

    1.启动hive报错: Logging initialized using configuration in jar:file:/usr/local/hive-1.2.1/lib/hive-commo ...

  4. MVC Pager使用

    View中加入引用:@using Webdiyer.WebControls.Mvc; 使用时代码: <div id="Pagination"> @Html.Pager( ...

  5. 908G New Year and Original Order

    传送门 分析 代码 #include<iostream> #include<cstdio> #include<cstring> #include<string ...

  6. Python使用signal模块实现定时执行

    在liunx系统中要想每隔一分钟执行一个命令,最普遍的方法就是crontab了,如果不想使用crontab,经同事指点在程序中可以用定时器实现这种功能,于是就开始摸索了,发现需要一些信号的知识... ...

  7. 回顾2017系列篇(五):人工智能给UI/UX设计带来的影响

    如今,我们正处于设计新纪年的转折点上,用机器人和人工智能方面的专家说法表达即“The end is near(终点近了)”.但这并不意味着世界末日,但未来机器人将毫无疑问地接管一部分目前被人类占领的工 ...

  8. Ubuntu14.04 下安装Vmware-Tools

    1.切换到ubuntu 图形界面 startx , 点击虚拟机菜单栏-安装VMware Tools 2. 在Ubuntu系统中找到VMwareTools-9.2.2-893683.tar.gz ,右键 ...

  9. 项目中遇到的死锁问题: Lock wait timeout exceeded; try restarting transaction

    最近项目中频繁出现  Lock wait timeout exceeded; try restarting transaction这个错误,把我们弄得痛苦不堪啊,为了解决问题,上网上找好多资料,终于把 ...

  10. 编写高质量代码改善C#程序的157个建议——建议152:最少,甚至是不要注释

    建议152:最少,甚至是不要注释 以往,我们在代码中不写上几行注释,就会被认为是钟不负责任的态度.现在,这种观点正在改变.试想,如果我们所有的命名全部采用有意义的单词或词组,注释还有多少存在的价值. ...