描述
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
1 | class Solution(object): |