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

  • () has score 1
  • AB has score A + B, where A and B are balanced parentheses strings.
  • (A) has score 2 * A, where A is a balanced parentheses string.

Example 1:

Input: "()"
Output: 1

Example 2:

Input: "(())"
Output: 2

Example 3:

Input: "()()"
Output: 2

Example 4:

Input: "(()(()))"
Output: 6

Note:

  1. S is a balanced parentheses string, containing only ( and ).
  2. 2 <= S.length <= 50

这个可以利用栈,在官方题解上:

Intuition and Algorithm

Every position in the string has a depth - some number of matching parentheses surrounding it. For example, the dot in (()(.())) has depth 2, because of these parentheses: (__(.__))

Our goal is to maintain the score at the current depth we are on. When we see an opening bracket, we increase our depth, and our score at the new depth is 0. When we see a closing bracket, we add twice the score of the previous deeper part - except when counting (), which has a score of 1.

For example, when counting (()(())), our stack will look like this:

  • [0, 0] after parsing (
  • [0, 0, 0] after (
  • [0, 1] after )
  • [0, 1, 0] after (
  • [0, 1, 0, 0] after (
  • [0, 1, 1] after )
  • [0, 3] after )
  • [6] after )

C++代码:

class Solution {
public:
int scoreOfParentheses(string S) {
stack<int> s;
s.push();
for(char c:S){
if(c == '(')
s.push();
else{
int v = s.top();
s.pop();
int w = s.top();
s.pop();
s.push(w + max(*v,));
}
}
return s.top();
}
};

(栈)leetcode856 Score of Parentheses的更多相关文章

  1. leetcode-856 Score of Parentheses

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

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

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

  3. Leetcode 856. Score of Parentheses 括号得分(栈)

    Leetcode 856. Score of Parentheses 括号得分(栈) 题目描述 字符串S包含平衡的括号(即左右必定匹配),使用下面的规则计算得分 () 得1分 AB 得A+B的分,比如 ...

  4. LC 856. Score of Parentheses

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

  5. [Swift]LeetCode856. 括号的分数 | Score of Parentheses

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

  6. [LeetCode] 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 括号的分数

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

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

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

  9. 856. Score of Parentheses

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

随机推荐

  1. vue2 mint-ui loadmore(下拉刷新)

    <template> <div class="page-loadmore"> <h1 class="page-title"> ...

  2. Lodop打印项对象类型属性区分

    Lodop提供了一些打印项类型,默认是普通项,通过设置打印对象的类型,可以实现一些普通项不能实现的效果.例如:该博客另一篇博文 标题是Lodop打印控件 打印‘接下一页’‘以下空白’,就用了眉脚项. ...

  3. The Bug and Exception of Hibernate

    1: hibernate4.3.5 的@oneToOne注解有Bug,建议不使用该版本,或者使用该版本不使用@table   annotation. 2:今天在用hibernate4.2.13的时候, ...

  4. Linux压缩和解压命令

    zip命令: 压缩 :zip -r files.zip fileFolder 解压:unzip files.zip tar命令: 压缩:tar -cvf files.tar fileFolder 解压 ...

  5. CSS3 flexbox 布局 ---- flex 容器属性介绍

    flexbox布局是CSS3中新增的属性,它可以很轻松地帮我们解决掉一些常见的布局问题,比如导航栏. 我们用普通的方法写导航栏,通常会在ul, li 结构写好后,让li 元素左浮动,然后再给ul 清浮 ...

  6. 高度可配置的 Linux 内存守护程序 Nohang!

    导读 Nohang 是一个 Linux 守护程序,也是一个高度可配置的 OOM(内存溢出)阻止工具,适用于 Linux 系统,能够有效地防止内存不足的情况. 部分功能特性 具有良好注释的配置文件,配置 ...

  7. winserver 2008 R2服务器安装IIS

    winserver 2008 R2 IIS7 安装IIS 打开服务器管理器 选择“角色”,右击添加角色 点击“下一步” 勾选”Web服务器(IIS)“,点击”下一步“ 勾选”常见Http功能.应用程序 ...

  8. Matplotlib学习---用matplotlib画散点图,气泡图(scatter plot, bubble chart)

    Matplotlib里有两种画散点图的方法,一种是用ax.plot画,一种是用ax.scatter画. 一. 用ax.plot画 ax.plot(x,y,marker="o",co ...

  9. Ionic3的http请求如何实现token验证,并且超时返回登录页

    要求 后台提供的接口,不能让人随便输入个链接就能访问,而是要加入一个token,token是动态的,每次访问的时候判断,有权限并且未过期的时候才可以访问接口. 后台的设计是 在登录的时候,首先要pos ...

  10. Java8的Stream语法详解(转载)

    1. Stream初体验 我们先来看看Java里面是怎么定义Stream的: A sequence of elements supporting sequential and parallel agg ...