1. 题目描述
注: 回溯法
2. 代码
1 class Solution: 2 def generateParenthesis(self, n: int) -> List[str]: 3 y = list() 4 string = '(' 5 L = 1 6 R = 0 7 self.BackTrack(L,R,n,string,y) 8 return y 9 10 def BackTrack(self,L,R,n,string,y): 11 if L == R == n: 12 y.append(string) 13 elif L == n and R < n: 14 self.BackTrack(L,R+1,n,string + ')',y) 15 elif L < n and R < n: 16 self.BackTrack(L+1,R,n,string + '(',y) 17 if L > R: 18 self.BackTrack(L,R+1,n,string + ')',y) 19 return
思路: