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 ...
随机推荐
- SAP FTP FOR ABAP programing
近来忙的不可开交,忙的一塌糊涂,呵呵,今天怀揣愧疚之心,上来分享博文一篇,算是对自己的一点安慰. 首先在SAP系统中提供了很多的FTP示例程序,如下: RSFTP001 SAPFT ...
- 安装jdk-windows系统
1. 明确需要安装的jdk版本,注意jdk存在小版本号,例如jdk1.7_51,如果不清楚小版本号的话建议安装最新版本的jdk: 2. 打开cmd命令窗口,输入java -version查看本机是否安 ...
- 在.NET Core 中实现健康检查
.NET Core中提供了开箱即用的运行状况检查,首先,我将在.NET Core API应用程序中执行运行状况检查,接下来,我们将使用DbContext集成SQL Server或数据库的运行状况检查, ...
- 全栈性能测试修炼宝典-JMeter实战笔记(一)
了解性能测试 性能测试不仅能够定位.分析问题,还要把握系统性能变化趋势:性能测试工程师能够帮助解决性能问题,搞定测试过程中的各种不合理配置,给出专业的优化建议. 第一章 性能方向职业发展 软件测试职业 ...
- [从源码学设计] Flume 之 memory channel
[从源码学设计] Flume 之 memory channel 目录 [从源码学设计] Flume 之 memory channel 0x00 摘要 0x01 业务范畴 1.1 用途和特点 1.2 C ...
- 活动精彩实录 | 王峰:Cassandra在360的多场景应用及未来趋势
点击此处观看完整活动视频 大家好,我是360的王峰,我今天主要通过Cassandra在多场景下的应用来介绍一下Cassandra在360落地的情况. 我会从以下这几个方面进行介绍.首先介绍下Cassa ...
- 二:SpringBoot-配置Log4j2,实现不同环境日志打印
SpringBoot-配置Log4j2,实现不同环境日志打印 日志打印之外观模式 1.日志配置 2.Log4j2的配置文件 3.简单的测试程序 日志打印之外观模式 每一种日志框架都有自己单独的API, ...
- Office系列---将Office文件(Word、PPT、Excel)转换为PDF文件,提取Office文件(Word、PPT)中的所有图片
将Office文件转换为PDF文件,提取Office文件中的所有图片 1.Office系列---将Office文件(Word.PPT.Excel)转换为PDF文件 1.1 基于Office实现的解决方 ...
- 关于项目中Spring版本与jdk1.8兼容问题解决
问题描述: 在用公司的工具搭建项目时,由于使用Spring框架,同时要求jdk版本为1.8:当部署完成以后,项目总是无法运行,会报错: Context namespace element 'annot ...
- 截止9月20日,xx行动中已知漏洞
VMware Fusion cve-2020-3980权限提升 Apache Cocoon security vulnerability cve-2020-11991 Spring框架RFD(文件下载 ...