Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.
给括号的对数 把所有可能的情况输出出来
dfs
class Solution:
# @param {integer} n
# @return {string[]}
def generateParenthesis(self, n):
str = []
res = []
if n == 0:
return []
def dfs(left,right):
##print left,right,str
if left == n and right == n:
res.append(''.join(str))
if left < n :
str.append('(')
dfs(left+1,right)
if right < left :
str.append(')')
dfs(left,right+1)
if str != []:
str.pop()
dfs(0,0)
return res