LC 856. Score of Parentheses
2024-09-04 07:48:02
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
Runtime: 0 ms, faster than 100.00% of C++ online submissions for Score of Parentheses.
(A) = 2 * A 是表示深度的一个概念。
class Solution {
public:
int scoreOfParentheses(string S) {
int ret = , cnt = ;
char last = ' ';
for(auto ch : S){
if(ch == '('){
cnt++;
}else {
cnt--;
if(last == '('){
ret += (<<cnt);
}
}
last = ch;
}
return ret;
}
};
最新文章
- ENode 1.0 - Saga的思想与实现
- LinkIssue: Error &#39;LINK : fatal error LNK1123: failure during conversion to COFF: file invalid or cor
- 软件快速开发平台 JEPF
- 怎样用ZBrush快速雕刻皮肤纹理
- codeforces 468B 2-sat
- sourceInsight的技巧
- jquery事件链式写法
- c#委托实例化和调用语句
- Android应用性能优化方案
- sql server 权限
- Oracle12c:支持通过创建identity columen来实现创建自增列
- 路由网关---zuul
- file 文件上传后缀转化小写
- 红黑树与AVL
- vue框架搭建
- mac下查看jdk安装版本及安装目录
- Cocos2d 编译js为jsc bytecode文件
- Vue2学习笔记:实例生命周期
- poj 3294 后缀数组 多字符串中不小于 k 个字符串中的最长子串
- 如何在 Debian 9 下安装 LEMP 和 WHMCS 7.5