给定一个括号数量n,生成所有可能的括号组合字符串
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
__title__ = ''
__author__ = 'thinkreed'
__mtime__ = '2017/3/24'
"""
class Solution(object):
def generateParenthesis(self, n):
"""
:type n: int
:rtype: List[str]
idea from https://discuss.leetcode.com/topic/17510/4-7-lines-python/2
"""
def generate(p, left, right, parens=[]):
if left: generate(p + '(', left - 1, right)
if right > left: generate(p + ')', left, right - 1)
if not right: parens += p,
return parens
return generate('', n, n)