LeetCode 856. 括号的分数(Score of Parentheses)
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
提示:
- S 是平衡括号字符串,且只含有 ( 和 ) 。
- 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;
}
}
参考资料
- https://leetcode.com/problems/score-of-parentheses/
- https://leetcode-cn.com/problems/score-of-parentheses/
LeetCode 856. 括号的分数(Score of Parentheses)的更多相关文章
- [Swift]LeetCode856. 括号的分数 | Score of Parentheses
Given a balanced parentheses string S, compute the score of the string based on the following rule: ...
- LeetCode:括号的分数【856】
LeetCode:括号的分数[856] 题目描述 给定一个平衡括号字符串 S,按下述规则计算该字符串的分数: () 得 1 分. AB 得 A + B 分,其中 A 和 B 是平衡括号字符串. (A) ...
- Leetcode 856. Score of Parentheses 括号得分(栈)
Leetcode 856. Score of Parentheses 括号得分(栈) 题目描述 字符串S包含平衡的括号(即左右必定匹配),使用下面的规则计算得分 () 得1分 AB 得A+B的分,比如 ...
- LC 856. Score of Parentheses
Given a balanced parentheses string S, compute the score of the string based on the following rule: ...
- LeetCode 856. Score of Parentheses 括号的分数
其实是这道题的变式(某港带同学的C/C++作业) 增加一点难度,输入的S不一定为平衡的,需要自己判断是否平衡,若不平衡输出为0. 题目描述 Given a parentheses string s, ...
- [LeetCode] Score of Parentheses 括号的分数
Given a balanced parentheses string S, compute the score of the string based on the following rule: ...
- 【LeetCode】856. Score of Parentheses 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 栈 递归 计数 日期 题目地址:https://le ...
- LeetCode 22. 括号生成(Generate Parentheses)
22. 括号生成 22. Generate Parentheses 题目描述 给出 n 代表生成括号的对数,请你写出一个函数,使其能够生成所有可能的并且有效的括号组合. 例如,给出 n = 3,生成结 ...
- 856. Score of Parentheses
Given a balanced parentheses string S, compute the score of the string based on the following rule: ...
随机推荐
- HBase 基本入门篇
无论是 NoSQL,还是大数据领域,HBase 都是非常”炙热”的一门数据库.本文将对 HBase 做一些基础性的介绍,旨在入门. 一.简介 HBase 是一个开源的.面向列的非关系型分布式数据库,目 ...
- ArrayList 集合 简单运用
集合 遍历 import java.util.ArrayList; class Demo02 { public static void main(String[] args) { // 创建Arra ...
- 【区间dp】P1063 能量项链
一道区间dp的水题 题目链接 来快活啊! 思路 很简单的区间dp,思路和floyed差不多,就是需要把项链处理成环形 用\(f[l][r]\)表示以\(a[l]\)开头\(a[r]\)结尾的数串的最大 ...
- 【loj2985】【WC2019】I君的商店
题目 交互题: 有\(n\)个物品,每个物品的价格为0或者1; 给出为1的物品的个数奇偶性k,并保证至少有一个价格为1: 每次可以询问一个集合S的另一个集合T的价值和的大小,交互库会返回>=或者 ...
- 干货 | 10分钟掌握branch and cut(分支剪界)算法原理附带C++求解TSP问题代码
00 前言 branch and cut其实还是和branch and bound脱离不了干系的.所以,在开始本节的学习之前,请大家还是要务必掌握branch and bound算法的原理. 01 应 ...
- Alpha冲刺(4/6)
队名:無駄無駄 组长博客 作业博客 组员情况 张越洋 过去两天完成了哪些任务 摸鱼 提交记录(全组共用) 接下来的计划 沟通前后端成员,监督.提醒他们尽快完成各自的进度 学习如何评估代码质量 准备Al ...
- python的tecplot模块(tecplot新特性)
Python的tecplot模块仅支持tecplot2017以上 首先安装tecplot2017 安装完成以后,安装python的tecplot模块 可以通过终端转入tecplot安装目录下的pyte ...
- Win10系统下安装ubuntu16.04双系统-常见问题解答
Win10系统下安装ubuntu16.04双系统-常见问题解答 1. 安装ubuntu16.04.2系统 磁盘分区形式有两种:GPT和MBR,关系到设置引导项.在win10下压缩出500GB空间给ub ...
- 【大数据】分布式文件系统HDFS 练习
作业要求来自于https://edu.cnblogs.com/campus/gzcc/GZCC-16SE2/homework/3292 利用Shell命令与HDFS进行交互 以”./bin/dfs d ...
- JVM探究之 —— OOM异常
在Java虚拟机规范的描述中,除了程序计数器外,虚拟机内存的其他几个运行时区域都有发生OutOfMemoryError(下文称OOM)异常的可能.本节探究主要基于jdk1.8的内存结构. 1. Jav ...