856. Score of Parentheses
Given a balanced parentheses string
S
, compute the score of the string based on the following rule:
()
has score 1AB
has scoreA + B
, where A and B are balanced parentheses strings.(A)
has score2 * A
, where A is a balanced parentheses strings
Example 1:
Input: "()"
Output: 1Example 2:
Input: "(())"
Output: 2Example 3:
Input: "()()"
Output: 2Example 4:
Input: "(()(()))"
Output: 6
Note:
S
is a balanced parentheses string, containing only(
and)
.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的更多相关文章
- Leetcode 856. Score of Parentheses 括号得分(栈)
Leetcode 856. Score of Parentheses 括号得分(栈) 题目描述 字符串S包含平衡的括号(即左右必定匹配),使用下面的规则计算得分 () 得1分 AB 得A+B的分,比如 ...
- LC 856. Score of Parentheses
Given a balanced parentheses string S, compute the score of the string based on the following rule: ...
- LeetCode 856. Score of Parentheses 括号的分数
其实是这道题的变式(某港带同学的C/C++作业) 增加一点难度,输入的S不一定为平衡的,需要自己判断是否平衡,若不平衡输出为0. 题目描述 Given a parentheses string s, ...
- 【LeetCode】856. Score of Parentheses 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 栈 递归 计数 日期 题目地址:https://le ...
- LeetCode 856. 括号的分数(Score of Parentheses)
856. 括号的分数 856. Score of Parentheses 题目描述 给定一个平衡括号字符串 S,按下述规则计算该字符串的分数: () 得 1 分. AB 得 A + B 分,其中 A ...
- [LeetCode] Score of Parentheses 括号的分数
Given a balanced parentheses string S, compute the score of the string based on the following rule: ...
- [Swift]LeetCode856. 括号的分数 | Score of Parentheses
Given a balanced parentheses string S, compute the score of the string based on the following rule: ...
- (栈)leetcode856 Score of Parentheses
Given a balanced parentheses string S, compute the score of the string based on the following rule: ...
- leetcode-856 Score of Parentheses
Given a balanced parentheses string S, compute the score of the string based on the following rule: ...
随机推荐
- ElasicSearch(4) 与jest结合
https://spring.io/projects/spring-data-elasticsearch https://docs.spring.io/spring-data/elasticsearc ...
- MySQL索引原理及优化
一.各种数据结构介绍 这一小节结合哈希表.完全平衡二叉树.B树以及B+树的优缺点来介绍为什么选择B+树. 假如有这么一张表(表名:sanguo): (1)Hash索引 对name字段建立哈希索引: 根 ...
- java 流转换BASE64的一些问题
java 转换BASE64过程中,出现很多结尾为空的问题!暂时不清楚为什么会这样- ``` java //根据url地址转换成BASE64 public static String getURLIma ...
- 记一个jquery 无缝轮播的制作方法
接触前端也很久了,今天才发现,要做好一个轮播,其实有很多东西需要考虑进去,否则做出来的轮播效果并不好,下面我就来做一个轮播,是依赖jquery来写的 1.要做轮播,首先需要的是HTML的内容,css的 ...
- Hibernate 再接触 性能优化
Sessionclear 否则session缓存里越来越多 Java有内存泄露吗? 在语法中没有(垃圾自动回收) 但是在实际中会有 比如读文件没有关什么的 1+N问题 解决方法:把fetch设置为la ...
- vs2015单步调试问题(附加进程)
如果页面有Codebehind的页属,那么前端通过ajax提交到后端代码,无法在后端代码中取到值. 这是一个vs属性标记,用于跟踪管理项目.如果后端代码的自定义指定(Inherits)的话,应该取掉 ...
- 1、detail页面 /items/detail/:id
<template> <div class="item_detail"> <van-swipe :autoplay="3000" ...
- autoperfixer 版本配置
{ "private": true, "dependencies": { "autoprefixer": "^9.3.1" ...
- centos下通过pid查看进程的绝对路径的方法
例如: 我想要知道我执行中的mysql路径 netstat -nlp pid拿到15330,然后 cd /proc/15330 由于linux在启动一个进程时,会在/proc下创建一个以PID命名的文 ...
- datatables插件提示Cannot reinitialise DataTable的解决办法
这个错误是由于重新设置数据源,又没有将原来的数据清空导致的. 网上有很多解决方案,试了都不管用. 最后找到一种方法,将原来的table销毁,再初始化. 方法是在datatable初始化的时候加入属性 ...