【Leetcode】python - [1614] Maximum Nesting Depth of the Parentheses 個人解法筆記

整理 LeetCode #1614 的個人解法筆記:解題思路、Time/Space Complexity 與邊界條件。

題目出處

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,並更新最大計數
  2. 遇到「)」 -1

Time Complexity

O(n)

Space Complexity

O(1)

Licensed under CC BY-NC-SA 4.0
最後更新 Sep 28, 2026
使用 Hugo 建立
主題 Stack 由 Jimmy 設計