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.

Example 1:

Input: "()"
Output: 1

Example 2:

Input: "(())"
Output: 2

Example 3:

Input: "()()"
Output: 2

Example 4:

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

Note:

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

Runtime: 0 ms, faster than 100.00% of C++ online submissions for Score of Parentheses.

(A) = 2 * A 是表示深度的一个概念。

class Solution {
public:
  int scoreOfParentheses(string S) {
    int ret = 0, cnt = 0;
    char last = ‘ ‘;
    for(auto ch : S){
      if(ch == ‘(‘){
        cnt++;
      }else {
        cnt--;
        if(last == ‘(‘){
          ret += (1<<cnt);
        }
      }
      last = ch;
    }
    return ret;
  }
};

原文地址:https://www.cnblogs.com/ethanhong/p/10192920.html

时间: 2024-10-08 19:24:55

LC 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: "()(

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 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

[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

20/32/22/856/301/921 Parentheses 括号匹配或者生成题

20. https://leetcode.com/problems/valid-parentheses/description/ 32. https://leetcode.com/problems/longest-valid-parentheses/description/ 22. https://leetcode.com/problems/generate-parentheses/description/ 856. https://leetcode.com/problems/score-of-

【LeetCode】栈 stack(共40题)

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [20]Valid Parentheses [42]Trapping Rain Water [71]Simplify Path [84]Largest Rectangle in Histogram [85]Maximal Rectangle [94]Binary Tree Inorder Traversal [103]Binary Tree Zigzag Level

【LeetCode】字符串 string(共112题)

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [3]Longest Substring Without Repeating Characters [5]Longest Palindromic Substring [6]ZigZag Conversion [8]String to Integer (atoi) [10]Regular Expression Matching [12]Integer to Roman

[LC] 22. Generate Parentheses

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: [ "((()))", "(()())", "(())()", "()(())", "()()()" ] Tim