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 ...
随机推荐
- JMS监听Oracle AQ
该文档中,oracle版本为11g,jdk版本1.8,java项目为maven构建的springboot项目,springboot的版本为2.1.6,并使用了定时任务来做AQ监听的重连功能,解决由于外 ...
- 运行.bat执行sql文件 —— mysql
参考地址:https://www.cnblogs.com/dingjiaoyang/p/9990188.html 运行test.bat 执行 test.sql文件 test.bat: @ECHO OF ...
- PAT Advanced 1004 Counting Leaves
题目与翻译 1004 Counting Leaves 数树叶 (30分) A family hierarchy is usually presented by a pedigree tree. You ...
- 30分钟带你理解 Raft 算法
为什么需要 Raft? Raft 是什么? Raft 的目标 前置条件:复制状态机 Raft 基础 Leader 选举(选举安全特性) 日志复制(Leader只附加.日志匹配) 安全 学习资料 使用 ...
- redis修改requirepass 参数 改密码
1. 不重启redis如何配置密码? a. 在配置文件中配置requirepass的密码(当redis重启时密码依然有效). # requirepass foobared -> 修改成 : ...
- namedtuple
Python的namedtuple使用详解_kongxx的专栏-CSDN博客_namedtuple https://blog.csdn.net/kongxx/article/details/51553 ...
- 打开APP 04 | 网络通信:RPC框架在网络通信上更倾向于哪种网络IO模型? 2020-02-26 何小锋
打开APP 04 | 网络通信:RPC框架在网络通信上更倾向于哪种网络IO模型? 2020-02-26 何小锋
- 语言反射规则 - The Laws of Reflection
[译]Go反射的三个原则(官方博客) | seven的分享 https://sevenyu.top/2019/12/21/laws-of-reflection.html wilhg/The-Laws- ...
- WPF combobox设置默认选项不生效的问题
combobox 是常用的控件,当我们需要绑定设置默认选项时,往往会绑定 SelectedItem 属性去设置, 可是你会惊奇地发现SelectedItem的值绑定了, 它依旧是熟悉的模样 根据官方的 ...
- JS 实现一个实时动态校验,将输入格式错误的显示为红色背景
功能描述: 源码: 功能描述: 实时动态校验,如果输入的格式错误,将弹窗提示输入格式错误并将背景展示为红色. 源码: 前台: <hy:formfield name="cxfdl&quo ...