leetcode 856. 括号的分数(Score of Parentheses)

目录

  • 题目描述:
  • 示例 1:
  • 示例 2:
  • 示例 3:
  • 示例 4:
  • 解法:

题目描述:

给定一个平衡括号字符串 S,按下述规则计算该字符串的分数:

() 得 1 分。
AB 得 A + B 分,其中 A 和 B 是平衡括号字符串。

(A) 得 2 * A 分,其中 A 是平衡括号字符串。

示例 1:

输入: "()"
输出: 1

示例 2:

输入: "(())"
输出: 2

示例 3:

输入: "()()"
输出: 2

示例 4:

输入: "(()(()))"
输出: 6

提示:

  • S 是平衡括号字符串,且只含有 ( 和 ) 。
  • 2 <= S.length <= 50

解法:

class Solution {
public:
    int scoreOfParentheses(string S) {
        //( ( ( ) ( ) )
        //( 4 )
        // use -1 to represent '('
        stack<int> stk;
        for(char ch : S){
            if(ch == '('){
                stk.push(-1);
            }else{
                int val = stk.top();
                int tmp = 0;
                while(val != -1){
                    tmp += stk.top();
                    stk.pop();
                    val = stk.top();
                }
                stk.pop();
                if(tmp == 0){
                    stk.push(1);
                }else{
                    stk.push(2*tmp);
                }
            }
        }
        int res = 0;
        while(!stk.empty()){
            res += stk.top();
            stk.pop();
        }
        return res;
    }
};

原文地址:https://www.cnblogs.com/zhanzq/p/10953720.html

时间: 2024-08-30 18:22:38

leetcode 856. 括号的分数(Score of Parentheses)的相关文章

LeetCode 856. 括号的分数

给定一个平衡括号字符串 S,按下述规则计算该字符串的分数: () 得 1 分. AB 得 A + B 分,其中 A 和 B 是平衡括号字符串. (A) 得 2 * A 分,其中 A 是平衡括号字符串. 由于问题是递归定义的,所以可以很简单地用递归去解这个题. 用栈解决的话,如果遇到左括号,则入栈.如果遇到右括号,判断栈顶是不是右括号,如果是则说明是(),出栈并压数字1:否则说明是(A)型,将内部数字全部加起来再次入栈.最后栈内是各种数字,加起来就可以了. class Solution { pub

Leetcode 856. Score of Parentheses 括号得分(栈)

Leetcode 856. Score of Parentheses 括号得分(栈) 题目描述 字符串S包含平衡的括号(即左右必定匹配),使用下面的规则计算得分 () 得1分 AB 得A+B的分,比如()()得2分 (A) 得2A分, 比如(()())得2(1+1)分 测试样例 Example 1: Input: "()" Output: 1 Example 2: Input: "(())" Output: 2 Example 3: Input: "()(

LC 856. Score of Parentheses

Given a balanced parentheses string S, compute the score of the string based on the following rule: () 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. Exam

【leetcode刷题笔记】Longest Valid Parentheses

Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring. For "(()", the longest valid parentheses substring is "()", which has length = 2. Another example is &

[LeetCode系列]括号生成问题

给定n, 返回所有匹配的n对括号的可能形式. 如 给定 n = 3, 一个解集是: "((()))", "(()())", "(())()", "()(())", "()()()" 本题解法的思路是 使用栈seq保存经历的字符串状态; 使用栈valid保存对应的字符串中有效的括号对个数; 当seq不为空时(即回溯未结束): 当前的字符串和其中有效的括号对个数分别出栈; 1. 如果字符串长度等于待求解的长度,

[LeetCode] Score of Parentheses 括号的分数

Given a balanced parentheses string?S, compute the score of the string based on the following rule: ()?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. Exam

leetcode 856. Score of Parentheses

Given a balanced parentheses string S, compute the score of the string based on the following rule: () 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. Exam

856. Score of Parentheses

Given a balanced parentheses string S, compute the score of the string based on the following rule: () 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. Exam

LeetCode 22. 括号生成(Generate Parentheses)

题目描述 给出 n 代表生成括号的对数,请你写出一个函数,使其能够生成所有可能的并且有效的括号组合. 例如,给出 n =3,生成结果为: [ "((()))", "(()())", "(())()", "()(())", "()()()" ] 解题思路 利用回溯的思想递归的生成括号.具体来说记录当前剩余左括号数left,剩余右括号数right,当前的生成括号字符串s,进行如下操作: 若left为0,说明左括