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.
Example 1:

Input: "()"
Output: 1
Example 2:

Input: "(())"
Output: 2
Example 3:

Input: "()()"
Output: 2
Example 4:

Input: "(()(()))"
Output: 6
Note:

S is a balanced parentheses string, containing only ( and ).
2 <= S.length <= 50

对于当前字符,如果是‘(‘记录他的位置,如果是‘)‘
有两种状态,要么是() 要么是(()) 所以用个数组去维护与)对应的(之间的数的和。具体见代码

class Solution {
public:
    int scoreOfParentheses(string S) {
        int s[55] = {0};
        vector<int> v(55, 0);
        int top = 0;
        int left = 0;
        int sum = 0;
        for (int i = 1; i < S.size(); ++i) {
            if (S[i] == '(') s[++top] = i;
            else {
                left = s[top--];
                if (left == i-1) {
                    v[i] = 1;
                }
                else {
                    sum = 0;
                    for (int j = left; j < i; ++j) {
                        sum += v[j];
                        v[j] = 0;
                    }
                    v[i] = 2*sum;
                }

            }
        }
        return accumulate(v.begin(), v.end(), 0);
    }
};

原文地址:https://www.cnblogs.com/pk28/p/9220501.html

时间: 2024-10-04 07:48:31

leetcode 856. Score of Parentheses的相关文章

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

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

一天一道LeetCode (一)题目 Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. For example, given n = 3, a solution set is: "((()))", "(()())", "(())()", "()(())", "(

leetcode 之 Longest Valid Parentheses

leetcode中和括号匹配相关的问题共有三个,分别是: Valid Parentheses Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. The brackets must close in the correct order, "()" and "()[]{}" are a

leetcode题解:Valid Parentheses(栈的应用-括号匹配)

题目: Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" and "([)]&

[LeetCode] 032. Longest Valid Parentheses (Hard) (C++)

指数:[LeetCode] Leetcode 指标解释 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 032. Longest Valid Parentheses (Hard) 链接: 题目:https://oj.leetcode.com/problems/longest-valid-parentheses/ 代码(github):https://github.com/illuz/leetcode 题意: 问一个字

【leetcode系列】Valid Parentheses

很经典的问题,使用栈来解决,我这里自己实现了一个栈,当然也可以直接用java自带的Stack类. 自己实现的栈代码: import java.util.LinkedList; class StackOne { LinkedList<Object> data; int top; int maxSize; StackOne(int size) { // TODO Auto-generated constructor stub top = -1; maxSize = 100; data = new

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: 输入: &quo