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

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

参考资料:

https://leetcode.com/problems/score-of-parentheses/

原文地址:https://www.cnblogs.com/grandyang/p/10634116.html

时间: 2024-10-07 08:11:12

[LeetCode] 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 22. Generate Parentheses (括号生成)

题目标签:Backtracking 建立一个 HashMap 来记录 括号的 数量,利用DFS, 先用 左括号, 在用 右括号, 当 右括号用完的时候 返回.具体看code. Java Solution: Runtime:  1 ms, faster than 85.94 % Memory Usage: 39.5 MB, less than 20.00 % 完成日期:12/12/2019 关键点:HashMap class Solution { HashMap<Character, Intege

leetcode 20 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 20 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:Valid Parentheses - 合理的括号搭配

1.题目名称 Valid Parentheses(合理的括号搭配) 2.题目地址 https://leetcode.com/problems/valid-parentheses/ 3.题目内容 英文:Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. The brackets must close in the

[LeetCode] 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: "((()))", "(()())", "(())()", "()(())", "()()()" 给一个数字n,

[LeetCode]20 Valid Parentheses 有效的括号

[LeetCode]20 Valid Parentheses 有效的括号 Description Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. An input string is valid if: Open brackets must be closed by the same type of brac

【leetcode】Generate Parentheses

题目: 给定整数n,返回n对匹配的小括号字符串数组. For example, given n = 3, a solution set is: "((()))", "(()())", "(())()", "()(())", "()()()" 分析: 这种问题的模式是:1)问题的解有多个 ,2)每个解都是由多个有效的 "步骤" 组成的,3)变更以有解的某个或某些"步骤"

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