leetcode-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
想法:设立三个变量,一个变量result代表最终的运算结果,初始化为0,。一个变量left代表左括号的个数,并且初始为1。一个变量right代表有括号的个数,初始为0。如何是Example 1 和Example 3这种情况,直接统计匹配的个数即可。对于Example 2这种情况,其数学含义表示为2的次幂,此时只需统计左后括号的差值。
class Solution {
public:
int scoreOfParentheses(string S) {
;
;
;
int len = S.size();
== len)
return result;
; i <= len ; i++){
if(S[i] == '('){
left++;
}else{
right++;
] == '('){
result+=pow(,left-right);
}
}
}
return result;
}
};
leetcode-856 Score of Parentheses的更多相关文章
- Leetcode 856. Score of Parentheses 括号得分(栈)
Leetcode 856. Score of Parentheses 括号得分(栈) 题目描述 字符串S包含平衡的括号(即左右必定匹配),使用下面的规则计算得分 () 得1分 AB 得A+B的分,比如 ...
- LeetCode 856. Score of Parentheses 括号的分数
其实是这道题的变式(某港带同学的C/C++作业) 增加一点难度,输入的S不一定为平衡的,需要自己判断是否平衡,若不平衡输出为0. 题目描述 Given a parentheses string s, ...
- 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 解题报告(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: ...
- LeetCode 856. 括号的分数(Score of Parentheses)
856. 括号的分数 856. Score of Parentheses 题目描述 给定一个平衡括号字符串 S,按下述规则计算该字符串的分数: () 得 1 分. AB 得 A + B 分,其中 A ...
- [LeetCode] 032. Longest Valid Parentheses (Hard) (C++)
指数:[LeetCode] Leetcode 指标解释 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 032. Lon ...
- 【一天一道LeetCode】#22. Generate Parentheses
一天一道LeetCode (一)题目 Given n pairs of parentheses, write a function to generate all combinations of we ...
- 乘风破浪:LeetCode真题_022_Generate Parentheses
乘风破浪:LeetCode真题_022_Generate Parentheses 一.前言 关于括号的题目,我们已经遇到过了验证正确性的题目,现在让我们生成合法的括号列表,怎么办呢?想来想去还是递归比 ...
- 乘风破浪:LeetCode真题_020_Valid Parentheses
乘风破浪:LeetCode真题_020_Valid Parentheses 一.前言 下面开始堆栈方面的问题了,堆栈的操作基本上有压栈,出栈,判断栈空等等,虽然很简单,但是非常有意义. 二.Valid ...
随机推荐
- Retrofit 2.0 使用和原理
使用教程: http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2015/1016/3588.html retrofit2 与okhttp关系 ...
- mysql小试题
1. 用户登录日志表 xes_user_login_logs 如下: (1) 检索登录超过两次的用户ID(sql语句) select user_id from vvt_ceshi group by u ...
- 关于shortcut icon和icon
语句一:<link rel="shortcut icon" href="favicon.ico" /> 语句二<link rel=" ...
- BZOJ2337: [HNOI2011]XOR和路径(期望 高斯消元)
题意 题目链接 Sol 期望的线性性对xor运算是不成立的,但是我们可以每位分开算 设\(f[i]\)表示从\(i\)到\(n\)边权为1的概率,统计答案的时候乘一下权值 转移方程为 \[f[i] = ...
- js-ES6学习笔记-编程风格(1)
1.ES6提出了两个新的声明变量的命令:let和const.其中,let完全可以取代var,因为两者语义相同,而且let没有副作用. 2.var命令存在变量提升效用,let命令没有这个问题.建议不再使 ...
- @NotNull、@NotEmpty、@NotBlank的区别
Spring中@NotNull.@NotEmpty.@NotBlank的区别@NotNull:用于基本数据类型@NotEmpty:用于集合类@NotBlank:用于String上面
- Ajax 滚动异步加载数据
第一种情况:单个div滚动 HTML <body> <!-- search start --> <div class="search" #if($m_ ...
- 稳聘App设计图分享
摘要||潜心学习,无限开源,我是鸟窝,一只憨厚的鸟,联系我加微信:jkxx123321 很早期就想筹划上线一款招聘类App,一拖再拖,先做还没有上线. 下面的设计原图,为我UI徒弟所做,在此,表示万分 ...
- TestLink笔记(一):环境配置+安装
注:转载请加上原文链接,谢谢! 本文的安装环境是Windows操作系统. (一) 前期准备 1.XAMPP下载(下载5.6的版本) https://www.apachefriends.org/ ...
- Oracle EBS 银行账户API
创建银行 -- Create Bank DECLARE p_init_msg_list VARCHAR2(200); p_country_code VARCHAR2(200); p_bank_nam ...