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. Centos7安装Spark2.4

    准备 1.hadoop已部署(若没有可以参考:Centos7安装Hadoop2.7),集群情况如下(IP地址与之前文章有变动): hostname IP地址 部署规划 node1 172.20.0.2 ...

  2. NOIP爆炸记

    NOIP爆炸游记 Day 0 Day 1 T1 T2 T3 Day 2 T1 T2 T3 最后 Day 0 复习模板 + 做真题 + 方 Day 1 早上吃了一片面包,就进了考场- T1 Exm??这 ...

  3. LeetCode 1026. Maximum Difference Between Node and Ancestor

    原题链接在这里:https://leetcode.com/problems/maximum-difference-between-node-and-ancestor/ 题目: Given the ro ...

  4. Oracle 查看index是否失效

    一.普通索引是否失效 select * from dba_indexes s where s.owner  in ('ISMP','BOSS','PAY','ACCOUNT','SETTLE','TE ...

  5. learning armbian steps(11) ----- armbian 源码分析(六)

    接下来我们来分析一下uboot的编写过程: 从 lib/compilation.sh  89开始阅读: compile_uboot() { # not optimal, but extra clean ...

  6. Xamarin移动开发之路

    Xamarin入门 1.Xamarin开发及学习资源 2.Xamarin安装及调试 Xamarin.Forms 1.入门 [快速入门] 2.XAML 3.应用程序基础知识 [辅助功能]eg:大类型.高 ...

  7. Linux 系统管理——引导过程与服务控制

    一. 系统引导流程 1.开机自检(BIOS)(基本的输入输出系统) 2.MBR引导1.2. MBRIS 当从本机硬盘中启动系统时,首先根据硬盘第一个扇区中MBR (Master Boot Record ...

  8. 02-线性结构3 Reversing Linked List (25 分)

    Given a constant K and a singly linked list L, you are supposed to reverse the links of every K elem ...

  9. mysql 获取学生个人科目平均分

    mysql> select * from test; +----+----------+-------+-----------+ | id | name | score | subject | ...

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

    项目进展 这周就是对上周所列出的类进行具体实现.但是到目前为止还没有遇到一些实质性的问题.虽然感觉没有问题就是最大的问题,但是还是希望能够尽早发现bug并及时改掉. 目前已经完成前几个文件之间的架构, ...