快轉到主要內容
  1. LeetCode/

LeetCode 22: Generate Parentheses

·1 分鐘· ·
LeetCode Daily Medium Backtracking Recursion String Parentheses Combinatorics 回溯 遞迴 字串 括號 組合數學
Wei Yi Chung
作者
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
目錄

基本資料
#

難易度: Medium 第一次嘗試: 2025-08-10

  • 總花費時間:00:00.00

解題思路
#

這是一個回溯問題,我們需要生成所有有效的括號組合。關鍵洞察是確保在剩餘可用的位置中,右括號的數量始終小於或等於左括號的數量。這個約束條件保證了我們只生成有效的括號字串。

解法
#

這個解決方案使用回溯方法,具有以下關鍵約束:

  1. 左括號約束:如果 left > 0,我們可以添加左括號
  2. 右括號約束:只有當 right > left 時,我們才能添加右括號(確保有效性)
  3. 基本情況:當 leftright 都達到 0 時,我們有一個完整的有效字串

回溯函數追蹤:

  • left:剩餘要放置的左括號數量
  • right:剩餘要放置的右括號數量
  • s:正在構建的當前字串
class Solution:
    def generateParenthesis(self, n: int) -> List[str]:
        res = []

        def backtracking(left, right, s):

            nonlocal res

            if left == 0 and right == 0:
                res.append(s)
                return
            
            if left > 0:
                backtracking(left - 1, right, s + "(")
            if right > left:
                backtracking(left, right - 1, s + ")")

        backtracking(n, n, "")

        return res

時間複雜度:O(4^n/√n) - 卡塔蘭數增長 空間複雜度:O(n) 用於遞迴堆疊

收穫
#

  • 回溯效率:這種方法有效地生成所有有效組合而不重複
  • 基於約束的剪枝right > left 條件確保我們永遠不會生成無效的括號字串
  • 卡塔蘭數:有效括號組合的數量遵循卡塔蘭數序列
  • 遞迴結構:由於其組合性質,這個問題自然地適合遞迴解決方案

遇到的問題
#

相關文章

LeetCode 20: Valid Parentheses
·1 分鐘
LeetCode Daily Easy Stack String Parentheses Validation Algorithm Data-Structure 括號 堆疊 字串 驗證 演算法
使用堆疊解決有效括號問題
LeetCode 1456: Maximum Number of Vowels in a Substring of Given Length
·1 分鐘
LeetCode Daily Medium Sliding Window String Vowel Counting Fixed Window
LeetCode 解題紀錄 - 固定大小滑動窗口與元音計數
LeetCode 438: Find All Anagrams in a String
·2 分鐘
LeetCode Daily Medium Sliding Window Hash Table String Anagram Frequency Counting
LeetCode 解題紀錄 - 使用字符頻率的滑動窗口
LeetCode 567: Permutation in String
·2 分鐘
LeetCode Daily Medium Sliding Window String Permutation Frequency Counting Hash Table
LeetCode 解題紀錄 - 使用字符頻率的滑動窗口
LeetCode 150: Evaluate Reverse Polish Notation
·1 分鐘
LeetCode Daily Medium Stack Expression-Evaluation Reverse-Polish-Notation Postfix-Notation Mathematics Algorithm Data-Structure 逆波蘭表示法 堆疊 表達式求值 數學 演算法
使用堆疊解決逆波蘭表示法求值問題
LeetCode 739: Daily Temperatures
·1 分鐘
LeetCode Daily Medium Stack Monotonic-Stack Array Temperature Waiting-Time Algorithm Data-Structure 溫度 堆疊 陣列 等待時間 演算法
使用單調堆疊解決每日溫度問題