題目出處
1614. Maximum Nesting Depth of the Parentheses
難度
easy
題目分類
String, Stack, Bracket Sequences
2026-09-28 一刷
個人範例程式碼 - 一刷 (2026/09/28)
class Solution:
def maxDepth(self, s: str) -> int:
count = 0
max_count = 0
for c in s:
if c == '(':
count += 1
max_count = max(max_count, count)
elif c == ')':
count -= 1
return max_count
算法說明
單純的計數題
- 遇到「(」 +1,並更新最大計數
- 遇到「)」 -1
Time Complexity
O(n)
Space Complexity
O(1)