(栈)leetcode856 Score of Parentheses
Given a balanced parentheses string S
, compute the score of the string based on the following rule:
()
has score 1AB
has scoreA + B
, where A and B are balanced parentheses strings.(A)
has score2 * 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:
S
is a balanced parentheses string, containing only(
and)
.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的更多相关文章
- leetcode-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)
856. 括号的分数 856. Score of Parentheses 题目描述 给定一个平衡括号字符串 S,按下述规则计算该字符串的分数: () 得 1 分. AB 得 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: ...
- [Swift]LeetCode856. 括号的分数 | Score of Parentheses
Given a balanced parentheses string S, compute the score of the string based on the following rule: ...
- [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 括号的分数
其实是这道题的变式(某港带同学的C/C++作业) 增加一点难度,输入的S不一定为平衡的,需要自己判断是否平衡,若不平衡输出为0. 题目描述 Given a parentheses string s, ...
- 【LeetCode】856. Score of Parentheses 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 栈 递归 计数 日期 题目地址:https://le ...
- 856. Score of Parentheses
Given a balanced parentheses string S, compute the score of the string based on the following rule: ...
随机推荐
- environment variable
%ALLUSERSPROFILE% C:\ProgramData %APPDATA% C:\Users\cuthead\AppData\Roaming %COMMONPROGRAMFILES% C:\ ...
- JDK 与TOMCAT的安装详解
转自:http://www.jb51.net/article/51909.htm Tomcat7.0.22在Windows下详细配置过程 一.JDK1.7安装 1.下载jdk,下载地址:http:// ...
- 复习 LIS nlogn
参考:https://www.cnblogs.com/wxjor/p/5524447.html 最长下降只要把符号都倒过来就行 在栈中二分找第一个比当前值小的替换就行
- [ZJOI2007] 仓库建设
传送门:>HERE< 题意:有n个地点,每个地点有货物P[i]个,距离起点(地点0)的距离为x[i].在每个地点建立仓库需要费用c[i],现在需要在某些地点建设仓库,从而将货物转移到仓库里 ...
- IDEA+Maven+多个Module模块(创建多模块SpringBoot整合项目)
最近在学习springboot,先从创建项目开始,一般项目都是一个项目下会有多个模块,这里先创建一个最简单的实例,一个项目下有一个springboot模块项目提供web服务,引用另一个java项目(相 ...
- lemon special judge模板
/* argv[1]:输入文件 argv[2]:选手输出文件 argv[3]:标准输出文件 argv[4]:单个测试点分值 argv[5]:输出最终得分的文件 argv[6]:输出错误报告的文件 */ ...
- 【XSY2729】欧拉子图 无向图连通性 数学
题目大意 给你一个\(n\)个点\(m\)条边的无向图(可能有重边),对于这个图的边集的子集(一共有\(2^m\)个),如果其导出的子图的每个联通块内都存在欧拉回路,我们就把答案加上这个子图的边数的平 ...
- IDEA 不识别的MAVEN 项目应如何处理
有些人啊,上传到git的项目,根本不是项目而是一个文件夹,文件夹里边还有个文件夹那才是项目,IDEA 不会识别出它是项目来 这个时候,需要选择这个文件夹下的pom.xml 文件 右键 pom.xml然 ...
- Failed to load package MonoAndroidDesignerPackage
from : https://developercommunity.visualstudio.com/content/problem/160124/failed-to-load-package-mon ...
- 【LOJ6036】编码(2-sat)
[LOJ6036]编码(2-sat) 题面 LOJ 题解 很显然的一个暴力: 枚举每个串中的?是什么,然后把和它有前缀关系的串全部给找出来,不合法的连边处理一下,那么直接跑\(2-sat\)就做完了. ...