题目来源:

  https://leetcode.com/problems/generate-parentheses/


题意分析:

  题目输入一个整型n,输出n对小括号配对的所有可能性。比如说,如果输入3,那么输出"((()))", "(()())", "(())()", "()(())", "()()()"。


题目思路:

  ①不难发现,n为0的时候输出是空,而n = 1的时候,输出“()”

  ②当n > 1的时候,要使得整个括号字符串可以配对,那么与第一个配对的右括号把n - 1对括号分成了一个 i (0 <= i < n)对已配对的括号和 n - 1 - i对已配对的括号。那么把所有的右括号划分的情况全部列出来就可以了。由于这里的时间复杂度比较复杂,就不作分析了。

根据上述可以得到:

f(n) = ∑(   '(' + f(i) +')' + f(n - 1 - i)   )


代码(python):

class Solution(object):
def generateParenthesis(self, n):
"""
:type n: int
:rtype: List[str]
"""
ans = []
if n == 0:
return ans
if n == 1:
return ['()']
i = 0
while i < n:
tmp1 = self.generateParenthesis(i)
tmp2 = self.generateParenthesis(n - 1 - i)
for j in tmp1:
for k in tmp2:
ans.append('(' + j + ')' + k)
if len(tmp2) == 0:
ans.append('(' + j + ')')
if len(tmp1) == 0:
for k in tmp2:
ans.append('()' + k)
i += 1
return ans

转载请注明出处:http://www.cnblogs.com/chruny/p/4872830.html

[LeetCode]题解(python):022-Generate Parentheses的更多相关文章

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

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

  2. LeetCode 022 Generate Parentheses

    题目描述:Generate Parentheses Given n pairs of parentheses, write a function to generate all combination ...

  3. LeetCode 22. 括号生成(Generate Parentheses)

    22. 括号生成 22. Generate Parentheses 题目描述 给出 n 代表生成括号的对数,请你写出一个函数,使其能够生成所有可能的并且有效的括号组合. 例如,给出 n = 3,生成结 ...

  4. 【JAVA、C++】LeetCode 022 Generate Parentheses

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

  5. LeetCode 笔记系列五 Generate Parentheses

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

  6. leetcode第21题--Generate Parentheses

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

  7. LeetCode(22)Generate Parentheses

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

  8. 022 Generate Parentheses 生成括号

    给 n 对括号,写一个函数生成所有合适的括号组合.比如,给定 n = 3,一个结果为:[  "((()))",  "(()())",  "(())() ...

  9. N-Queens And N-Queens II [LeetCode] + Generate Parentheses[LeetCode] + 回溯法

    回溯法 百度百科:回溯法(探索与回溯法)是一种选优搜索法,按选优条件向前搜索,以达到目标.但当探索到某一步时,发现原先选择并不优或达不到目标,就退回一步又一次选择,这样的走不通就退回再走的技术为回溯法 ...

  10. leetcode022. Generate Parentheses

    leetcode 022. Generate Parentheses Concise recursive C++ solution class Solution { public: vector< ...

随机推荐

  1. bootstrap 导航栏

    非常好的一篇文章: http://webdesigntutsplus.s3.amazonaws.com/tuts/312_bs/My-Bootstrap-Site-NAVBAR/navbar-exam ...

  2. HDU 5274 Dylans loves tree(树链剖分)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5274 [题目大意] 给出一棵树,每个点有一个权值,权值可修改,且大于等于0,询问链上出现次数为奇数 ...

  3. java面试核心基础(1)

    1.以下代码的执行结果 String s1 = "helloworld"; String s2 = "hello" + new Stirng("wor ...

  4. iOS app 集成友盟推送问题

    之前做app推送主要是集成友盟SDK,在程序获取deviceToken时,老是提示如下错误: Error Domain=NSCocoaErrorDomain Code=3000 "未找到应用 ...

  5. compass安装教程

    *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !important; } /* ...

  6. unity 播放外部视频

    摘要: Unity支持的播放视频格式有.mov..mpg..mpeg..mp4..avi和.asf.只需将对应的视频文件拖拽入Project视图即可,它会自动生成对应的MovieTexture对象. ...

  7. ExtJs006类别名、备用名

    Ext.onReady(function () { //Ext.define 其他配置项 //别名.备用名 Ext.define("User", { config: { name: ...

  8. 深刻理解void,void*和sizeof关键字

    void的字面值是“无类型”,void*则是"无类型指针".void*可以指向任何类型的数据.void几乎只有"注释"和限制程序的作用,因为从来没有人会定义一个 ...

  9. C程序的存储空间布局

    历史沿袭至今,C程序一直由下列几部分组成: 1. 正文段.这是由CPU执行的机器指令部分.通常,正文段是可共享的,所以即使是频繁执行的程序(编辑器,编译器,命令解释器)在存储器中也只需一个副本,另外正 ...

  10. Volley 设置 RetryPolicy 不起作用, 重复提交

    RT, Google后有的说是 将超时时间设置为0, 但是还是会重试提交, 解决方案如下: static HurlStack stack = new HurlStack(){ @Override pr ...