您的位置:首页 > 其它

leetcode 022 Generate Parentheses

2015-06-13 23:22 375 查看
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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  dfs leetcode