括号生成

LeetCode Hot 100 回溯
难度: 中等

题目描述

给定一个数字 n,生成所有合法的括号组合。

解题思路

使用回溯法,递归生成左括号和右括号,确保合法性条件(左括号数量 >= 右括号数量)。时间复杂度为 O(2^n)。