题目:
题解:
class Solution:def generateParenthesis(self, n: int) -> List[str]:if n == 0:return []total_l = []total_l.append([None]) # 0组括号时记为Nonetotal_l.append(["()"]) # 1组括号只有一种情况for i in range(2,n+1): # 开始计算i组括号时的括号组合l = [] for j in range(i): # 开始遍历 p q ,其中p+q=i-1 , j 作为索引now_list1 = total_l[j] # p = j 时的括号组合情况now_list2 = total_l[i-1-j] # q = (i-1) - j 时的括号组合情况for k1 in now_list1: for k2 in now_list2:if k1 == None:k1 = ""if k2 == None:k2 = ""el = "(" + k1 + ")" + k2l.append(el) # 把所有可能的情况添加到 l 中total_l.append(l) # l这个list就是i组括号的所有情况,添加到total_l中,继续求解i=i+1的情况return total_l[n]