856. 括号的分数

856. Score of Parentheses

题目描述

给定一个平衡括号字符串 S,按下述规则计算该字符串的分数:

  • () 得 1 分。
  • AB 得 A + B 分,其中 A 和 B 是平衡括号字符串。
  • (A) 得 2 * A 分,其中 A 是平衡括号字符串。

LeetCode856. Score of Parentheses中等

示例 1:

输入: "()"
输出: 1

示例 2:

输入: "(())"
输出: 2

示例 3:

输入: "()()"
输出: 2

示例 4:

输入: "(()(()))"
输出: 6

提示:

  1. S 是平衡括号字符串,且只含有 ( 和 ) 。
  2. 2 <= S.length <= 50

Java 实现

import java.util.Stack;

class Solution {
public int scoreOfParentheses(String S) {
int cur = 0;
Stack<Integer> stack = new Stack<>();
for (char c : S.toCharArray()) {
if (c == '(') {
stack.push(cur);
cur = 0;
} else {
cur = stack.pop() + Math.max(cur * 2, 1);
}
}
return cur;
}
}

参考资料

LeetCode 856. 括号的分数(Score of Parentheses)的更多相关文章

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

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

  2. LeetCode:括号的分数【856】

    LeetCode:括号的分数[856] 题目描述 给定一个平衡括号字符串 S,按下述规则计算该字符串的分数: () 得 1 分. AB 得 A + B 分,其中 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. LeetCode 856. Score of Parentheses 括号的分数

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

  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 解题报告(Python & C++)

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

  8. LeetCode 22. 括号生成(Generate Parentheses)

    22. 括号生成 22. Generate Parentheses 题目描述 给出 n 代表生成括号的对数,请你写出一个函数,使其能够生成所有可能的并且有效的括号组合. 例如,给出 n = 3,生成结 ...

  9. 856. Score of Parentheses

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

随机推荐

  1. es6 添加事件监听

    //定义被侦听的目标对象 }; //定义处理程序 var interceptor = { set: function (receiver, property, value) { console.log ...

  2. fitnesse wiki界面设置变量

    有时候我们可能多组测试数据会到同一个值,这样我们就可以设置一个变量,修改时只需要修改一个地方即可,而不需要对每组测试数据的这列数据进行修改 如下图: (1)定义变量:!define A {10}  , ...

  3. 第三章 - SQL基础及元数据获取

    SQL的介绍 SQL的定义:结构化查询语句 SQL的作用:对库和表进行操作 SQL的常用分类 DDL 数据定义语言(Data Definition Language) DCL 数据控制语言(Data ...

  4. P2210 Haywire

    P2210 Haywire 模拟退火练手题 #include<cmath> #include<ctime> #include<cstdio> #include< ...

  5. 计蒜客 39280.Travel-二分+最短路dijkstra-二分过程中保存结果,因为二分完最后的不一定是结果 (The 2019 ACM-ICPC China Shannxi Provincial Programming Contest M.) 2019ICPC西安邀请赛现场赛重现赛

    Travel There are nn planets in the MOT galaxy, and each planet has a unique number from 1 \sim n1∼n. ...

  6. GoCN每日新闻(2019-10-04)

    GoCN每日新闻(2019-10-04) 国庆专辑:GopherChina祝大家国庆节快乐 GoCN每日新闻(2019-10-04) 1. Go提议流程:代表 https://research.swt ...

  7. maven 私服上有jar包但是却下载不下来

    解决办法: 在parent中执行deploy命令就解决了. 原因:第一次建项目,上传jar包的时候直接进入到该项目中进行deploy到私服.最终发现私服仓库有,但是别人引用的时候无法下载.是因为别人下 ...

  8. 2015-2016-2《Java程序设计》团队博客4

    一.类结构图 这一周将所有的类都进行了实现,以下是这周实现的类图:         二.项目进展 目前已经将所有代码都编写完成,正在进行整体测试.虽然期间遇到了一些问题,但我们一起进行了讨论,并查找了 ...

  9. Jenkins 设置 源码库浏览器

    玩转Jenkins - 源码库浏览器功能如何使用 - 简书https://www.jianshu.com/p/22f72effb4ef Jenkins上踩过的那些坑 - 简书https://www.j ...

  10. html网页调用本地exe程序的实现方法(转)

    https://blog.csdn.net/ilovecr7/article/details/46803711 最近在做一个项目,要什么网页里调exe...开始以为不能实现,后来想想很多就跟淘宝网页上 ...