LeetCode 856. Score of Parentheses 括号的分数
其实是这道题的变式(某港带同学的C/C++作业)
增加一点难度,输入的S不一定为平衡的,需要自己判断是否平衡,若不平衡输出为0.
题目描述
Given a parentheses string s, compute the score of the string based on the following rule:
• If s is not balanced, the score is 0.
• () has score 1.
• AB has score A + B, where A and B are balanced parentheses strings.
• (A) has score 2 * A, where A is a balanced parentheses string.
A balanced string satisfies the following requirements:
• The number of ‘(’ equals the number of ‘)’ in the string.
• Counting from the first position to any position in the string, the number of ‘(’ is
greater than or equal to the number of ‘)’ in the string. (For example, ‘)(’ is not
balanced.)
The length of s is at most 30.
Input:
The input is a parentheses string s.
Output:
Print the score of s.
Examples:
1. Input:
(())
Output:
2
2. Input:
(()(()))
Output:
6
3. Input:
((()())
Output:
0
一种简单的方法
#include<iostream>
#include<string>
using namespace std;
int scoreOfParentheses(string S) {
int lef=0,flag=0; int cnt = 0;
int res = 0;
char last = ' ';
char ch;
for (int i=0;i<S.length();i++) {
ch=S[i];
if (ch == '(') {
cnt++;
lef++;
}
else {
// 深度变小
cnt--;
lef--;
// 上一个是'('表名这一对是(),可以加分
if (last == '(') {
res += 1 << cnt;
}
}
// 记录字符
if(lef<0)
{
flag=1;
break;
}
last = ch;
}
if(flag||(lef>0))
res=0;
return res;
}
int main()
{
string s;
cin>>s;
int ans=scoreOfParentheses(s);
cout<<ans;
system("pause");
return 0;
}
一种利用了规定好形式的栈的方法
#include <iostream>
#include <string>
#include <stack>
#include<cstdlib>
using namespace std; struct node {
int sc;
char type;
node(int x): sc(x) ,type('p') {}
};
stack<node> A;
void SolveC() {
string s;
cin >> s;
int n = s.length();
int score = 0;
int temp = 0;
int temp1 = 0;
node* nnode;
for (int i = 0; i <= (n - 1); i++) {
if (s[i] == '(') {
nnode = new node(0);
nnode->type = '(';
A.push(*nnode); }
if (s[i] == ')')
{
if (A.empty()) {
cout << 0;
return;
}
if ( A.top().type == '(') {
A.pop();
nnode = new node(0);
nnode->sc = 1;
nnode->type='p';//add type
// if (A.top().type == '(' || A.empty()) {
/*if (A.empty()||A.top().type == '(' ) {
A.push(*nnode);
}*/
A.push(*nnode);
temp=0; //ADD
if (A.top().type == 'p') {
while (!A.empty() && (A.top().type == 'p') ) {
temp1 = A.top().sc;
A.pop();
temp = temp + temp1;
}
nnode = new node(0);
nnode->sc = temp;
nnode->type='p';
A.push(*nnode);
}
continue; //ADD
}
if ( A.top().type == 'p'){
temp = A.top().sc;
A.pop();
if(A.empty()==1) //
{
cout<<0;
return;
}
if (A.top().type == '(') {
A.pop();
temp = temp * 2;
while (!A.empty() && (A.top().type == 'p')) {
temp1 = A.top().sc;
A.pop();
temp = temp + temp1;
} nnode = new node(0);
nnode->sc = temp;
nnode->type='p';
A.push(*nnode);
} }
else {
cout << 0;
return;
}
}
} score = A.top().sc; //检查是否((过多
A.pop(); if(A.empty());
else
{
score=0;
}
cout << score; return; } int main() {
SolveC();
system("pause");
return 0;
}
LeetCode 856. Score of Parentheses 括号的分数的更多相关文章
- Leetcode 856. Score of Parentheses 括号得分(栈)
Leetcode 856. Score of Parentheses 括号得分(栈) 题目描述 字符串S包含平衡的括号(即左右必定匹配),使用下面的规则计算得分 () 得1分 AB 得A+B的分,比如 ...
- [LeetCode] Score of Parentheses 括号的分数
Given a balanced parentheses string S, compute the score of the string based on the following rule: ...
- 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 ...
- 【LeetCode】22. Generate Parentheses 括号生成
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:括号, 括号生成,题解,leetcode, 力扣,Pyt ...
- 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】
LeetCode:括号的分数[856] 题目描述 给定一个平衡括号字符串 S,按下述规则计算该字符串的分数: () 得 1 分. AB 得 A + B 分,其中 A 和 B 是平衡括号字符串. (A) ...
- [LeetCode] 032. Longest Valid Parentheses (Hard) (C++)
指数:[LeetCode] Leetcode 指标解释 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 032. Lon ...
随机推荐
- Api文档自动生成工具
java开发,根据代码自动生成api接口文档工具,支持RESTful风格,今天我们来学一下api-doc的生成 作者:互联网编程. 欢迎投稿,一起交流技术 https://www.jianshu.co ...
- JavaWeb三大框架基础架构——CRUD的基础功能搭建
@ 目录 介绍 注意 applicationContext.xml mybatis-config.xml web.xml 结束语 介绍 项目前端采用了bootstrap,后端是ssm三大框架 注意 这 ...
- feign使用okHttpClient,调用原理
最近项目中 spring cloud 用到http请求,使用feign,配置okhttp,打算配置一下就直接使用,不过在压测与调优过程中遇到一些没有预测到的问题,附上排查与解析结 yml.pom配置 ...
- 全栈性能测试修炼宝典-JMeter实战笔记(二)
性能测试初体验 性能测试实质:利用工具去模拟大量用户操作来验证系统能够承受的负载情况,找出潜在的性能问题,分析并解决:找出系统性能变化趋势,为后续的扩展提供参考 测试分类 测试内容中,负载测试.压力测 ...
- Web自动化测试python环境中安装 --selenium安装、火狐和火狐驱动版本、谷歌和谷歌驱动版本、测试
一.安装selenium Windows命令行(cmd)输入pip install selenium(无须指定版本默认最新)或 pip install selenium==3.141.0(可指定版本) ...
- RSA2对于所有商户都是单独一对一的,并且只支持开发平台密钥管理和沙箱
蚂蚁金服开发者社区 https://openclub.alipay.com/club/history/read/1495 RSA 和 RSA2 签名算法区别 - 支付宝开放平台 https://ope ...
- get uuid
https://wx2.qq.com/?&lang=zh_CN /** * 启动二维码登录 */ function doQrcodeLogin() { loginFactory.getUUID ...
- 详解Go中内存分配
转载请声明出处哦~,本篇文章发布于luozhiyun的博客:https://www.luozhiyun.com 本文使用的go的源码15.7 介绍 Go 语言的内存分配器就借鉴了 TCMalloc 的 ...
- LOJ1036
AHOI 2008 聚会 Y 岛风景美丽宜人,气候温和,物产丰富.Y 岛上有 N 个城市,有 N-1 条城市间的道路连接着它们.每一条道路都连接某两个城市.幸运的是,小可可通过这些道路可以走遍 Y 岛 ...
- python atexit模块学习
python atexit模块 只定义了一个register模块用于注册程序退出时的回调函数,我们可以在这个函数中做一下资源清理的操作 注:如果程序是非正常crash,或者通过os._exit()退出 ...