LeetCode 22. Generate Parentheses--Python 解法--广度优先、深度优先解法
此文首发于我的个人博客: LeetCode 22. Generate Parentheses–Python 解法–广度优先、深度优先解法 — zhang0peter的个人博客 LeetCode题解文章分类: LeetCode题解文章集合 LeetCode 所有题目总结: LeetCode 所有题目总结 题目地址: Generate Parentheses - LeetCode Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. For example, given n = 3, a solution set is: [ "((()))", "(()())", "(())()", "()(())", "()()()" ] 这道题目看起来不难,只要把所有的可能性都输出即可。 可以深度优先,或者广度优先解决。 广度优先的Python解法如下,先输出 ()()()... class Solution : def generateParenthesis ( self , n : int ) - > List [ str ] : def helper ( s = '' , left = 0 , n = 0 ) : if n == 0 :