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:

[
"((()))",
"(()())",
"(())()",
"()(())",
"()()()"
]

思路:向string 中插入( 和 ),每插入一个就减1。 那么如何保证这个combination 是正确的呢?

  1. 插入数量不超过n

  2. 可以插入 ) 的前提是 ( 的数量大于 )

所以就得到了递归的两个条件。

 class Solution(object):
def generateParenthesis(self, n):
"""
:type n: int
:rtype: List[str]
"""
res = []
def help(s,left,right):
if(left==0 and right==0):
res.append(s[:])
return
if left>0:
help(s+'(',left-1,right)
if right>left:
help(s+')',left,right-1)
help('',n,n)
return res

22. Generate Parentheses(回溯)的更多相关文章

  1. 刷题22. Generate Parentheses

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

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

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

  3. 22. Generate Parentheses(ML)

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

  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. 22. Generate Parentheses C++回溯法

    把左右括号剩余的次数记录下来,传入回溯函数. 判断是否得到结果的条件就是剩余括号数是否都为零. 注意判断左括号是否剩余时,加上left>0的判断条件!否则会memory limited erro ...

  7. 【一天一道LeetCode】#22. Generate Parentheses

    一天一道LeetCode (一)题目 Given n pairs of parentheses, write a function to generate all combinations of we ...

  8. 22. Generate Parentheses产生所有匹配括号的方案

    [抄题]: Given n pairs of parentheses, write a function to generate all combinations of well-formed par ...

  9. LeetCode OJ 22. Generate Parentheses

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

随机推荐

  1. 改变PS1的颜色

    我们能够通过配置PS1变量使提示符成为彩色.在PS1中配置字符序列颜色的格式为:       \[\e[F;Bm\]       基本上是夹在 "\e["(转义开方括号)和 &qu ...

  2. N32903系列的基础知识(1)

    N32903U1DN使用ARM926EJ-S内核,其内部集成的JPEG编解码器.CMOS摄像头接口.32通道的声音处理单元(SPU).ADC.DAC等不仅可以满足各种的应用需求,还能减少生产方面的物料 ...

  3. UITouch 的主要方法:

    1. UITouch 的主要方法: - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event; - (void)touchesM ...

  4. net 中的一些知识

    这是一篇摘抄的文章 有一些内容对我很有帮助 .有一些内容解释很清晰 所以我拿过来了. 第一遍用了5天时间,第二遍看的时候决定自己复制一份出来于是有了这儿博客. 什么是.NET?什么是.NET Fram ...

  5. 【BZOJ1879】[Sdoi2009]Bill的挑战 状压DP

    [BZOJ1879][Sdoi2009]Bill的挑战 Description Input 本题包含多组数据.  第一行:一个整数T,表示数据的个数.  对于每组数据:  第一行:两个整数,N和K(含 ...

  6. HDU 4597 Play Game(DFS,区间DP)

    Play Game Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others) Total Sub ...

  7. C++ string append方法的常用用法

    append函数是向string的后面追加字符或字符串. 1).向string的后面加C-string string s = “hello “; const char *c = “out here “ ...

  8. 数据库们~MySQL~MongoDB~Redis

    mysql基础 mysql进阶 python操作mysql MongoDB Redis

  9. B - Network---UVA 315(无向图求割点)

        A Telephone Line Company (TLC) is establishing a new telephone cable network. They are connectin ...

  10. 十六.MySQL存储过程

    1.创建一个没有参数的存储过程 CREATE PROCEDURE sp1() SELECT VERSION(); 调用存储过程:CALL sp1(); 2.带有IN参数的存储过程 CREATE PRO ...