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 strings

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

Approach #1. DFS. [Java]

class Solution {
public int scoreOfParentheses(String S) {
return helper(S, 0, S.length() - 1);
} public int helper(String S, int l, int r) {
if (l + 1 == r) return 1;
int b = 0;
for (int i = l; i < r; ++i) {
if (S.charAt(i) == '(') b++;
else b--;
if (b == 0) return helper(S, l, i) + helper(S, i+1, r);
}
return 2 * helper(S, l + 1, r - 1);
} }

  

Approach #2: Stack. [Java]

class Solution {
public int scoreOfParentheses2(String S) {
boolean mode = true;
int ret = 0;
Stack<Character> stack = new Stack<>();
for (int i = 0; i < S.length(); ++i) {
if (S.charAt(i) == ')' && mode) {
ret += Math.pow(2, stack.size() - 1);
mode = false;
stack.pop();
} else if (S.charAt(i) == '(') {
stack.push('(');
mode = true;
} else {
stack.pop();
}
}
return ret;
} }

  

856. Score of Parentheses的更多相关文章

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

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

  2. LC 856. Score of Parentheses

    Given a balanced parentheses string S, compute the score of the string based on the following rule: ...

  3. LeetCode 856. Score of Parentheses 括号的分数

    其实是这道题的变式(某港带同学的C/C++作业) 增加一点难度,输入的S不一定为平衡的,需要自己判断是否平衡,若不平衡输出为0. 题目描述 Given a parentheses string s, ...

  4. 【LeetCode】856. Score of Parentheses 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 栈 递归 计数 日期 题目地址:https://le ...

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

    856. 括号的分数 856. Score of Parentheses 题目描述 给定一个平衡括号字符串 S,按下述规则计算该字符串的分数: () 得 1 分. AB 得 A + B 分,其中 A ...

  6. [LeetCode] Score of Parentheses 括号的分数

    Given a balanced parentheses string S, compute the score of the string based on the following rule: ...

  7. [Swift]LeetCode856. 括号的分数 | Score of Parentheses

    Given a balanced parentheses string S, compute the score of the string based on the following rule: ...

  8. (栈)leetcode856 Score of Parentheses

    Given a balanced parentheses string S, compute the score of the string based on the following rule: ...

  9. leetcode-856 Score of Parentheses

    Given a balanced parentheses string S, compute the score of the string based on the following rule: ...

随机推荐

  1. SSM商城项目(八)

    1.   学习计划 1.solr集群搭建 2.使用solrj管理solr集群 3.把搜索功能切换到集群版 4.添加商品同步到索引库 2.   什么是SolrCloud SolrCloud(solr 云 ...

  2. vivo手机,自带世界之窗浏览器,sessionStorage设置的值为null

    最近遇到一个小问题 用vivo手机自带浏览器,每次跳转到一个新页面都是重新打开一个webpage,导致sessionStorage设置的值都没了. 所以开发移动端网页时,谨慎使用sessionStor ...

  3. Html利用CSS布局技巧

    单列布局水平居中 水平居中的页面布局中最为常见的一种布局形式,多出现于标题,以及内容区域的组织形式,下面介绍四种实现水平居中的方法(注:下面各个实例中实现的是child元素的对齐操作,child元素的 ...

  4. Windbg驱动双机调试环境配置

    [由于进入了Windows驱动编程领域第一步就是搭建环境,整个环境来说说难也不难,只是比较麻烦.文章有些地方比较繁琐的,而且别人写的比较好,作为引用参考直接贴连接了.如果你按照我写的一步步完成,很快就 ...

  5. 迭代器模块 itertools

    无限迭代器 itertools 包自带了三个可以无限迭代的迭代器.这意味着,当你使用他们时,你要知道你需要的到底是最终会停止的迭代器,还是需要无限地迭代下去. 这些无限迭代器在生成数字或者在长度未知的 ...

  6. Python+Selenium学习--案例介绍

    1. 前言 前面讲解了那么多selenium的基础知识,下面用一个简单案例来介绍,此案例主要实现,运行测试,自动生成html报告,并发生邮件. 2. 测试案例 2.1 目录结构介绍 conf:配置信息 ...

  7. mybatis动态排序

    如果我们要传入排序字段作为一个参数到mybatis中,用以实现按照指定字段来排序的功能,那么我们需要使用$,而不是像其他参数一样,使用#.如下所示. <if test="sortnam ...

  8. 258. Add Digits 入学考试:数位相加

    [抄题]: Given a non-negative integer num, repeatedly add all its digits until the result has only one ...

  9. vue 关于父组件无法触发子组件的事件的解决方法

    一般情况导致无法触发子组件的方法  基本都是由于子组件未渲染完成 就进行了调用,解决方法如下: 1.加定时器  setTimeout(() => { //加定时器原因是,子组件页面未渲染处理就做 ...

  10. JVM总括四-类加载过程、双亲委派模型、对象实例化过程

    JVM总括四-类加载过程.双亲委派模型.对象实例化过程 目录:JVM总括:目录 一. 类加载过程 类加载过程就是将.class文件转化为Class对象,类实例化的过程,(User user = new ...