文章目录:
题目:
给出 n 代表生成括号的对数,请你写出一个函数,使其能够生成所有可能的并且有效的括号组合。
例如,给出 n = 3,生成结果为:
[
"((()))",
"(()())",
"(())()",
"()(())",
"()()()"
]
脚本一:【用时:36ms】
class Solution: def generateParenthesis(self, n: int) -> List[str]: lists = [] if n >0 and not lists :lists.append("(") while lists: zuo = lists[0].count("(") you = lists[0].count(")") if zuo > you: if zuo == n: lists.append(lists[0]+")") lists.pop(0) else: lists.append(lists[0] + ")") lists.append(lists[0] + "(") lists.pop(0) else: if zuo == n: return(lists) else: lists.append(lists[0] + "(") lists.pop(0)
脚本一逻辑:
shell解题分享:
https://blog.csdn.net/weixin_43428906/article/details/102790053
此链接是笔者较早时间使用shell解决此题的文章,分享给有兴趣的朋友
原文:https://www.cnblogs.com/mailong/p/12037807.html