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: ...
随机推荐
- 百度OCR文字识别-Android安全校验
本文转载自好基友upuptop:https://blog.csdn.net/pyfysf/article/details/86438769 效果图: 如下为文章正文: 百度OCR接口使用总结:之前总结 ...
- file 的类型 input
上传你选择的文件和相关信息.在 HTML 文档中 <input type="file"> 标签每出现一次,一个 FileUpload 对象就会被创建.该元素包含一个文本 ...
- 洛谷 P2004 领地选择 题解
P2004 领地选择 题目描述 作为在虚拟世界里统帅千军万马的领袖,小Z认为天时.地利.人和三者是缺一不可的,所以,谨慎地选择首都的位置对于小T来说是非常重要的. 首都被认为是一个占地C*C的正方形. ...
- C Primer Plus AND 菜鸟教程
C语言概述 首先,windows 环境下安装 GCC编译环境 下载 MinGW 下载地址:http://sourceforge.net/projects/mingw/files/ 根据系统环境下载对应 ...
- 【loj2568】【APIO2016】【学习笔记 左偏树】烟花表演
题目 一棵树,\(n\)个非叶子节点,编号为\(1-n\),\(m\)个叶子节点,编号为\(n+1-n+m\) 每条边有边权,修改边权的代价为\(|a-b|\) ; 定义一个叶子的距离为到1(根节点) ...
- delphi开第二个进程报错cannot create file editorlineends.ttr
网上说问题是windows系统补丁造成的,解决办法有卸补丁.装插件,还有自己搞个bat启动. 在网上看到最好的一个办法是: 把这个文件EditorLineEnds.ttr的后缀改为ttf,然后安装这个 ...
- Css3美化【让你的网页独一无二!】
一.span标签:能让某几个文字或者某个词语凸显出来 <p> 今天是11月份的<span>第一天</span>,地铁卡不打折了 </ ...
- ARC063F すぬけ君の塗り絵 2 / Snuke's Coloring 2
题面 一句话题面:给你一些点,求这些点之中夹的最大的矩形周长.(考虑边界) Solution 首先是一个结论,答案矩形一定经过\(x=\frac{w}{2}\)或经过\(y=\frac{h}{2}\) ...
- 下载根目录下的pdf文件, 浏览器下载
public void outPut(HttpServletRequest request, HttpServletResponse response, Integer type) { ClassPa ...
- css3学习之--transition属性(过渡)
一.理解transition属性 W3C标准中对CSS3的transition是这样描述的: CSS的transition允许CSS的属性值在一定的时间区间内平滑地过渡.这种效果可以在鼠标单击,获得焦 ...