数字 n 代表生成括号的对数,请你设计一个函数,用于能够生成所有可能的并且 有效的 括号组合。
示例 1:
输入:n = 3
输出:["((()))","(()())","(())()","()(())","()()()"]
示例 2:
输入:n = 1
输出:["()"]
提示:
1 <= n <= 8
class Solution: def generateParenthesis(self, n: int) -> List[str]: res = [["None"], ["()"]] for i in range(2, n+1): tmp = [] for j in range(i): q = i - 1 - j list1 = res[j] list2 = res[q] for a in list1: for b in list2: if a == "None": a = "" if b == "None": b = "" tmp.append("(" + a + ")" + b) res.append(tmp) return res[n]
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)