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:

[
"((()))",
"(()())",
"(())()",
"()(())",
"()()()"
]
class Solution(object):
def generateParenthesis(self, n):
"""
:type n: int
:rtype: List[str]
[ "((()))",
"(()())",
"(())()",
"()(())",
"()()()"]
()
/ \
() ()
/ \ / \
() ()()()
"""
ans = []
path = []
self.gen_par_helper(n*2, path, ans)
return ans def is_valid_par(self, par):
stack = []
for c in par:
if c == "(":
stack.append("(")
else:
if stack:
stack.pop()
else:
return False
return len(stack) == 0 def gen_par_helper(self, n, path, ans):
if n == 0:
if self.is_valid_par(path):
ans.append("".join(path))
return
for c in "()":
path.append(c)
self.gen_par_helper(n-1, path, ans)
path.pop()
class Solution(object):
def generateParenthesis(self, n):
"""
:type n: int
:rtype: List[str]
[ "((()))",
"(()())",
"(())()",
"()(())",
"()()()"]
()
/ \
() ()
/ \ / \
() ()()()
"""
ans = []
self.gen_par_helper(n, n, "", ans)
return ans def gen_par_helper(self, left, right, path, ans):
if left == 0 and right == 0:
ans.append(path)
return
if left > 0:
self.gen_par_helper(left-1, right, path+"(", ans)
if right > 0 and right > left:
self.gen_par_helper(left, right-1, path+")", ans)

第二种更快,只是不那么容易想到!

22. Generate Parentheses——本质:树,DFS求解可能的path的更多相关文章

  1. [Leetcode][Python]22: Generate Parentheses

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 22: Generate Parentheseshttps://oj.leet ...

  2. 22. Generate Parentheses(ML)

    22. Generate Parentheses . Generate Parentheses Given n pairs of parentheses, write a function to ge ...

  3. 刷题22. Generate Parentheses

    一.题目说明 这个题目是22. Generate Parentheses,简单来说,输入一个数字n,输出n对匹配的小括号. 简单考虑了一下,n=0,输出"";n=1,输出" ...

  4. 【LeetCode】22. Generate Parentheses (2 solutions)

    Generate Parentheses Given n pairs of parentheses, write a function to generate all combinations of ...

  5. 22. Generate Parentheses (recursion algorithm)

    Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...

  6. leetcode@ [22]Generate Parentheses (递归 + 卡特兰数)

    https://leetcode.com/problems/generate-parentheses/ Given n pairs of parentheses, write a function t ...

  7. 22.Generate Parentheses[M]括号生成

    题目 Given n pairs of parentheses, write a function to generate all combinations of well-formed parent ...

  8. [LeetCode] 22. Generate Parentheses 生成括号

    Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...

  9. 【LeetCode】22. Generate Parentheses 括号生成

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:括号, 括号生成,题解,leetcode, 力扣,Pyt ...

随机推荐

  1. python_way,day8 面向对象【多态、成员--字段 方法 属性、成员修饰符、特殊成员、异常处理、设计模式之单例模式、模块:isinstance、issubclass】

    python_way day8 一.面向对象三大特性: 多态 二.面向对象中的成员 字段.方法属性 三.成员修饰符 四.特殊成员 __init__.__doc__.__call__.__setitem ...

  2. 超实用压力测试工具-ab工具

    在学习ab工具之前,我们需了解几个关于压力测试的概念 吞吐率(Requests per second)概念:服务器并发处理能力的量化描述,单位是reqs/s,指的是某个并发用户数下单位时间内处理的请求 ...

  3. HDU5807分段dp

    DAG图. [题意] n(50)个城市m(c(n,2))条单向边(x,y),保证x<y 对于三个点(x,y,z)如果abs(w[x]-w[y])<=K && abs(w[x ...

  4. poj3334Connected Gheeves(二分)

    链接 二分高度,算面积的地方有点麻烦,没有用求交点的模板,直接自己按三角形相似手算了一下,写的有点麻烦. 上下界直接取水可放的最高点以及最低点. 自己的长得很挫的代码 #include <ios ...

  5. (转)Web自动化测试中的接口测试

    1.背景 1.1 Web程序中的接口 1.1.1 典型的Web设计架构 web是实现了基于网络通信的浏览器客户端与远程服务器进行交互的应用,通常包括两部分:web服务器和web客户端.web客户端的应 ...

  6. Mysql delete,truncate,drop

    1.delete 是DML(Data Manipulation Language),每次删除一行,作为事务记录在日志,可以回滚.delete from xxx 2.truncate是DDL(Data ...

  7. js中event.keyCode用法及keyCode对照表

    HTML 用户名:<input type="text" id="UserAccount" onKeyPress="JumpByEnter(Use ...

  8. Objective-C与C++的区别

    1.两者的最大相同:都是从C演化而来的面相对象语言,两者都兼容标准C语言 2.两者的最大不同:Objective-C提供了运行期动态绑定机制,而C++是编译静态绑定,并且通过嵌入类(多重继承)和虚函数 ...

  9. Hbase之原子性插入

    /** * Created by similarface on 16/8/16. */ import java.io.IOException; import org.apache.hadoop.con ...

  10. 使用升级助 升级了win10,黑屏,无桌面 解决方案

    使用U盘重装即可. 事实证明,win10升级助手实在不咋地 优待又2: (1)保留原win7系统,有后悔药: (2)原系统的软件可用: 缺点: (1)装得慢,一上午: (2)开机慢,三分半 (3)开机 ...