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 ...
随机推荐
- SQL -去重Group by 和Distinct的效率
经实际测试,同等条件下,5千万条数据,Distinct比Group by效率高,但是,这是有条件的,这五千万条数据中不重复的仅仅有三十多万条,这意味着,五千万条中基本都是重复数据. 为了验证,重复数据 ...
- java进阶(33)--IO流
一.IO流概念:1.基本概念2.IO流分类3.java.io流的四大家族4.流的close和flush方法5.java.id下常用的16个流 二.FileInputStream字节输入流1.FileI ...
- Py装饰器
装饰器: 1.定义,什么是装饰器 装饰器本质是一个函数,它是为了给其他函数添加附加功能 2.装饰器的两个原则 原则1 不修改被修饰函数的源代码原则2 不修改被修饰函数的调用方式 3.首先来看一 ...
- MAX232数据方向
在调试一个新板子的时候,串口调试从来都是放在前面的,而由于是一个新板子,电路图也是新的,因此有时候不知道串口线结对了没有,这个时候可能会对照PCB和原理图去看一下,但有时候看的人会很迷糊,因为不同的人 ...
- ROS教程(二):创建工作空间(图文)
ros教程:创建工作空间 目录 前言 一.工作空间? 二.创建一个工作空间 1.创建目录 2.编译 前言 使用catkin创建一个工作空间 一.工作空间? 在ROS系统下,我们所有的项目都放在一个工作 ...
- Spring AOP介绍与使用
Spring AOP介绍与使用 AOP:Aspect Oriented Programming 面向切面编程 OOP:Object Oriented Programming 面向对象编程 面向切面 ...
- 架构风格 vs. 架构模式 vs. 设计模式(译)
4.架构风格 vs. 架构模式 vs. 设计模式(译) - 简书 https://www.jianshu.com/p/d8dce27f279f
- Server Tracking of Client Session State Changes Connection Management
MySQL :: MySQL 8.0 Reference Manual :: 5.1.12 Connection Management https://dev.mysql.com/doc/refman ...
- linux系统层面调优
linux系统层面调优和常见的面试题 - 云+社区 - 腾讯云 https://cloud.tencent.com/developer/article/1664287
- Redis下载及安装(windows版)
下载地址1.Github下载地址:https://github.com/MicrosoftArchive/redis/releases2.百度网盘下载地址 https://pan.baidu.com/ ...