LC 856. Score of Parentheses
Given a balanced parentheses string S, compute the score of the string based on the following rule:
()has score 1ABhas 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:
Sis 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;
}
};
LC 856. Score of Parentheses的更多相关文章
- Leetcode 856. Score of Parentheses 括号得分(栈)
Leetcode 856. Score of Parentheses 括号得分(栈) 题目描述 字符串S包含平衡的括号(即左右必定匹配),使用下面的规则计算得分 () 得1分 AB 得A+B的分,比如 ...
- 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】856. Score of Parentheses 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 栈 递归 计数 日期 题目地址:https://le ...
- LeetCode 856. 括号的分数(Score of Parentheses)
856. 括号的分数 856. Score of Parentheses 题目描述 给定一个平衡括号字符串 S,按下述规则计算该字符串的分数: () 得 1 分. AB 得 A + B 分,其中 A ...
- [LeetCode] 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: ...
- (栈)leetcode856 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
Given a balanced parentheses string S, compute the score of the string based on the following rule: ...
随机推荐
- [转]DELL PERC 系列阵列卡选型和用法指南
引用地址 https://www.sulabs.net/?p=895 DELL PERC 系列阵列卡选型和用法指南 2018年12月29日 Su 本文缘起于一位朋友在生产服务器硬件中,使用了错误的阵列 ...
- Atmel芯片使用
ATMEL系列芯片 9X35 9G35可pin-to-pin替代9G10,具体需核对.此外即使pin-to-pin替代,外部应用也不一样. A5D2处理器,可支持linux/andriod. M7(M ...
- visio的形状默认是蓝色的填充色,怎么设置为白色为默认色?
如图所示: 设计->主题->选黑白那个 效果如下:
- Networker软件安装
- hivesql-一个表中的数据不在另一个表中
如何最有效的判断 一个表中的数据不在另一个表中 两个方法一个是join 另一个是 exist 方法
- HDU-1358-Period(KMP, 循环节)
链接: https://vjudge.net/problem/HDU-1358#author=0 题意: For each prefix of a given string S with N char ...
- Acwing-203-同余方程(扩展欧几里得)
链接: https://www.acwing.com/problem/content/205/ 题意: 求关于x的同余方程 ax ≡ 1(mod b) 的最小正整数解. 思路: 首先:扩展欧几里得推导 ...
- CodeForces 830B - Cards Sorting
将每个数字的位置存进该数字的vector中 原数组排个序从小到大处理,每次在vector里二分找到距离当前位置“最远”的位置(相差最大),更新答案 树状数组维护每个数字现在的位置和原位置之差 #inc ...
- (转载)了解Android 4.1,之三:黄油项目 —— 运作机理及新鲜玩意
Welcome back to GTKA, everyone's favorite investigative series where we learn all about the newest v ...
- Confluence 6 删除一个附加的文件
你需要具有 删除附件(Delete Attachment)的空间权限来删除一个附加的文件. 希望删除一个附加文件的所有版本: 进入含有附件的页面中. Go to > Attachments 选 ...