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. CUBRID学习笔记 14 dll加载错误

    这个问题通常是缺少文件cascci.dll 或者版本错误 32 64弄错了 C:\Program Files (x86)\Python266>python.exe Python 2.6.6 (r ...

  2. Android——例子:屏幕切换

    效果图如下:                          Xml文件代码: <?xml version="1.0" encoding="utf-8" ...

  3. s表达式和json表达式

    s表达式 + 1 2 3普通表达式 1+2+3json表达式{ +:[1, 2, 3]}优点,一个运算符,无限个参数 s表达式 * (+ 1 2) 3普通表达式 1+(2*3)json表达式{ *:[ ...

  4. CSS3 transition/transform

    Transition 1.简写属性transition,可以包括四个属性,这四个属性的顺序按照下面介绍的顺序书写,即transition:property duration timing-functi ...

  5. 网页自适应@media

    @media (min-width: 768px){ }/*屏幕最小为768px时调用括号里的属性*/ @media (max-width: 767px) {} /*屏幕最大为768px时调用括号里的 ...

  6. Android中利用SharedPreferences保存信息

    package com.example.sharepreferen; import android.content.Context; import android.content.SharedPref ...

  7. web设计经验<五>国外设计师总结的7个找灵感实用方法

    每个设计师不时会有创意灵感缺失.大脑一片空白的状态.盯着一个空白的屏幕,发愁着“好吧,我现在该做什么呢?该怎么做呢?”有些人喜欢静待,但这不是唯一的一个方法.焦虑的客户或者是你自己的不耐烦,都会让你无 ...

  8. vitamio框架

    import io.vov.vitamio.LibsChecker; import io.vov.vitamio.MediaPlayer; import io.vov.vitamio.MediaPla ...

  9. ios 开发之 Xcode6 No valid signing identities (i.e. certificate and private key pair) matching...

    之前的项目用证书真机测试过,我想再无证书Build,出现下面的报错提示! 下面的team我无法改成None!一点击None选的还是Unhonw name(JPGE28K3W9)这个是报错的关键 最后由 ...

  10. 【转载】【Oracle 11gR2】db_install.rsp详解

    [原文]http://blog.csdn.net/jameshadoop/article/details/48086933 ###################################### ...