题目来源:

  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. fastDFS同步问题讨论

    一.文件同步延迟问题 前面也讲过fastDFS同组内storage server数据是同步的, Storage server中由专门的线程根据binlog进行文件同步.为了最大程度地避免相互影响以及出 ...

  2. Google市场推广统计

    Google Play作为Android最大的应用市场,也存在这推广等常用的行为,那么如何统计呢,Google Analytics SDK或者其他的SDK都提供了方法,实际上是可以不需要任何sdk,完 ...

  3. 【从零学习Python】Ubuntu14.10下Python开发环境配置

    1. 前言 近期在研究计算机视觉的一些算法,也刚開始接触linux,试着在ubuntu下用qt+openCV进行开发,感觉还行.可是Python作为在学术领域广为应用的高级解释性语言.其在计算机视觉的 ...

  4. Problem F: Exponentiation

    Problem F: ExponentiationTime Limit: 1 Sec Memory Limit: 128 MBSubmit: 4 Solved: 2[Submit][Status][W ...

  5. Andy's First Dictionary

    Description Andy, 8, has a dream - he wants to produce his very own dictionary. This is not an easy ...

  6. JavaScript 堆

    1.IE中不兼容ajax中data最后一个参数加逗号,其余chrome Firefox均支持. code: $("document ").ready(function() { $( ...

  7. ERROR 1044 (42000): Access denied for user 'root'@'localhost' to database 'information_schema'

    在我想把备份的数据库导入到本地数据的时候,发生这个错误,我使用的工具是dbForge Studio for MySQL ERROR 1044 (42000): Access denied for us ...

  8. codeforces 375D . Tree and Queries 启发式合并 || dfs序+莫队

    题目链接 一个n个节点的树, 每一个节点有一个颜色, 1是根节点. m个询问, 每个询问给出u, k. 输出u的子树中出现次数大于等于k的颜色的数量. 启发式合并, 先将输入读进来, 然后dfs完一个 ...

  9. Mongodb备份(mongodump)和恢复(mongorestore)

    1.备份: mongodump -d DbName -o /data/backup 2. 恢复: mongorestore -d newDB --drop data/backup/DbName/

  10. linux(ubuntu) 遇到的问题 --1

    1.使用sudo提示用户不在sudoers文件中的解决方法 切换到root用户 su root 查看/etc/sudoers文件权限,如果只读权限,修改为可写权限 [root@localhost ~] ...