LeetCode No.22 Posted on 2021-01-27 | In OJ , LeetCode | | LeetCode第二十二题 ¶题目描述 数字 n 代表生成括号的对数,请你设计一个函数,用于能够生成所有可能的并且 有效的 括号组合。 12345678910111213示例 1:输入:n = 3输出:["((()))","(()())","(())()","()(())","()()()"]示例 2:输入:n = 1输出:["()"] 提示:1 <= n <= 8 ¶代码 123456789101112131415161718192021222324252627class Solution(object): def func(self,strlist, string, l, r, n): # 单边递归结束条件 if l > n or r > n or r > l: return # string生成结束条件 if l == n and r == n: strlist.append(string) return self.func(strlist, string + '(', l+1, r, n) self.func(strlist, string + ')', l, r+1, n) return def generateParenthesis(self, n): """ :type n: int :rtype: List[str] """ strlist = [] self.func(strlist, "", 0, 0, n) return strlistif __name__ == '__main__': s = Solution() print(s.generateParenthesis(3)) Hobby lead creation, technology change world. Post author: StriveZs Post link: www.strivezs.com/2021/01/27/LeetCode%E7%AC%AC%E4%BA%8C%E5%8D%81%E4%BA%8C%E9%A2%98/ Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 3.0 unless stating additionally.