本文共 493 字,大约阅读时间需要 1 分钟。
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:
[ "((()))", "(()())", "(())()", "()(())", "()()()"]
把所有符合规则的()排列方式输出。
思路自己没想清楚,看了discuss。
用dfs的方法,l表示‘(’使用的数量,r表示‘)’使用的数量。
所以左括号<n的时候添加'(',右括号小于左括号时候添加")"
class Solution: def generateParenthesis(self, n: int) -> List[str]: final=[] l,r=0,0 def dfs(l,r,path): if l==r==n:final.append(path) if l
转载地址:http://lcrbb.baihongyu.com/