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 stri
classSolution{public:intscoreOfParentheses(stringS){intres =0, n = S.size();for(inti =0; i < n; ++i) {if(S[i] ==')')continue;intpos = i +1, cnt =1;while(cnt !=0) { (S[pos++] =='(') ? ++cnt : --cnt; }intcur = scoreOfParentheses(S.substr(i +1, pos - i -...
856. Score of Parentheses Medium Topics Companies Given a balanced parentheses string s, return the score of the string. The score of a balanced parentheses string is based on the following rule: "()" has score 1. AB has score A + B, where A and B are balanced parentheses strings. ...
则其对分数和的贡献为 2 ^ depth ,统计贡献即可 score += 2 ^ depth 。 时间复杂度:O(n) 需要遍历 s 中的全部 O(n) 个字符 空间复杂度:O(1) 只需要使用常数个额外变量 代码(Python3) class Solution: def scoreOfParentheses(self, s: str) -> int: # score 统计所有括号的分数和 score: int ...
leetcode 856. Score of Parentheses Given a balanced parentheses string S, compute the score of the string based on the following rule: () has score1AB has scoreA+B, whereAandBare balanced parentheses strings. (A) has score2*A, whereAisabalanced parentheses string....
因此,我们可以找到每一个 () 对应的深度 x,那么答案就是 2^x 的累加和。 代码语言:javascript 代码运行次数:0 运行 AI代码解释 classSolution(object):defscoreOfParentheses(self,S):ans=bal=0fori,xinenumerate(S):ifx=='(':bal+=1else:bal-=1ifS[i-1]=='(':ans+=1<<balreturnans...
class Solution { public int scoreOfParentheses(String s) { Deque<Character> deque = new ArrayDeque(); for (int i = 0; i < s.length(); i++) { if (s.charAt(i) == '(') deque.addLast('('); else { char c = deque.removeLast(); if (c == '(') { deque.addLast('1'); ...
Leetcode 856. Score of Parentheses 括号得分(栈) 题目描述 字符串S包含平衡的括号(即左右必定匹配),使用下面的规则计算得分 + 得1分 + 得A+B的分,比如 得
Given a balanced parentheses string , compute the score of the string based on the following rule: h
classSolution:defscoreOfParentheses(self,S):""" :type S: str :rtype: int """stack=[]forchinS:ifch=='(':stack.append(ch)else:num=0ifstack[-1]=='(':stack.pop()stack.append(1)else:whilestack[-1]!='(':num+=stack.pop()stack.pop()stack.append(num*2)returnsum(stack) ...