AI代码解释 classSolution(object):defscoreOfParentheses(self,S):ans=bal=0fori,xinenumerate(S):ifx=='(':bal+=1else:bal-=1ifS[i-1]=='(':ans+=1<<balreturnans
代码(Python3) class Solution: def scoreOfParentheses(self, s: str) -> int: # score 统计所有括号的分数和 score: int = 0 # 维护每一个括号当前的深度 depth: int = 0 # 带下标遍历每一个括号 for (i, ch) in enumerate(s): if ch == '(': # 如果当前是左括号,则深度加 1 depth +=...
https://github.com/grandyang/leetcode/issues/856 类似题目: Valid Parentheses 参考资料: https://leetcode.com/problems/score-of-parentheses/ https://leetcode.com/problems/score-of-parentheses/discuss/141777/C%2B%2BJavaPython-O(1)-Space [LeetCode All in One 题目讲解汇总(持续更新中...)](https...
classSolution(object):defscoreOfParentheses(self, S): ans= bal =0fori, xinenumerate(S):ifx =='(': bal+= 1else: bal-= 1ifS[i-1] =='(': ans+= 1 <<balreturnans 参考链接:https://leetcode-cn.com/problems/score-of-parentheses/solution/gua-hao-de-fen-shu-by-leetcode/...
https://github.com/grandyang/leetcode/issues/856 类似题目: Valid Parentheses 参考资料: https://leetcode.com/problems/score-of-parentheses/ https://leetcode.com/problems/score-of-parentheses/discuss/141777/C%2B%2BJavaPython-O(1)-Space [LeetCode All in One 题目讲解汇总(持续更新中...)](...
Minimum Remove to Make Valid Parentheses Leetcode 735. Asteroid Collision Hashmap/ Hashset题目: Leetcode 1. Two Sum Leetcode 146. LRU Cache (Python中可以使用OrderedDict来代替) Leetcode 128. Longest Consecutive Sequence Leetcode 73. Set Matrix Zeroes Leetcode 380. Insert Delete GetRandom O(1)...
032.Longest-Valid-Parentheses (H) 155.Min-Stack (M) 225.Implement Stack using Queues (H-) 232.Implement-Queue-using-Stacks (H-) 341.Flatten-Nested-List-Iterator (M) 173.Binary-Search-Tree-Iterator (M) 536.Construct-Binary-Tree-from-String (M) 456.132-Pattern (H-) 636.Exclusive-Time-...
236 Lowest Common Ancestor of a Binary Tree Medium Go Java 237 Delete Node in a Linked List Medium JavaScript 238 Product of Array Except Self Medium JavaScript Python 239 Sliding Window Maximum Hard Python 240 Search a 2D Matrix II Medium JavaScript 241 Different Ways to Add Parentheses Medium...
def scoreOfParentheses(self, S: str) -> int: stack = [] res = 0 for i in S: if i == '(': stack.append(res) res = 0 elif i == ')': p = stack.pop() res = p + 1 if res == 0 else p + 2 * res else:
https://shenjie1993.gitbooks.io/leetcode-python/032%20Longest%20Valid%20Parentheses.html 采用了动态规划,dp[i]表示以i为子字符串末尾时的最大长度,最后的结果就是dp中的最大值。如果不是空字符串,则dp[0]=0,因为一个括号肯定无法正确匹配。递推关系是: 代码语言:javascript 代码运行次数:0 运行 AI代...