题目来源:

  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. 解决libc.so.6: version `GLIBC_2.14' not found问题, 升级glibc,glibc-2.15

    0.以下在系统CentOS 6.3 x86_64上操作 1.试图运行程序,提示"libc.so.6: version `GLIBC_2.14' not found",原因是系统的g ...

  2. Windows Azure 社区新闻综述(#73 版)

    欢迎查看最新版本的每周综述,其中包含有关云计算和 Windows Azure的社区推动新闻.内容和对话. 以下是过去一周基于您的反馈汇集在一起的内容: 文章.视频和博客文章 ·   Windows A ...

  3. Java图形化界面设计——中间容器(Jpanel)

    1.  将组件添加到JFrame中 方式之一: frame.getContentPane().add(childComponent) 用getContentPane()方法获得JFrame的内容面板, ...

  4. 使用LAMP创建基于wordpress的个从博客站点

    參考: http://blog.csdn.net/ck_boss/article/details/27866117 一.mysql配置 1.安装mysql yum install mysql-serv ...

  5. windows 下提取目录下所有文件的文件名

    tree D:/dir /f >D:/filenames.txt 提取D盘dir目录下所有文件名,写入文件filenames.txt

  6. 打印 PHP $_SERVER 常量

    foreach( $_SERVER as $var => $value){ echo $var.' '.$value.'<br>'; };

  7. 如何实现HTTPSERVER

    Write your own http server author : Kevin Lynx Why write your own? 看这个问题的人证明你知道什么是http server,世界上有很多 ...

  8. [LeetCode]题解(python):128-Longest Consecutive Sequence

    题目来源: https://leetcode.com/problems/longest-consecutive-sequence/ 题意分析: 给定一个没有排好序的数组,找到最长的连续序列的长度.要求 ...

  9. contains 和 ele.compareDocumentPosition确定html节点间的关系

    ~~~ nodeA.contains(nodeB) //ie ,   nodeA.compareDocumentPosition(nodeB) //firefox opera 1.DOMElement ...

  10. vi 替换命令“找不到模式”解决

    在linux vi编辑工具中使用替换命令操作时,会出现明明有匹配查找模式的数据.却报"找不到模式"问题. 原因是vi s///替换操作缺省针对行,若要生效,则须要将光标移动到指定行 ...