[LeetCode]题解(python):022-Generate Parentheses
题目来源:
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的更多相关文章
- [Leetcode][Python]22: Generate Parentheses
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 22: Generate Parentheseshttps://oj.leet ...
- LeetCode 022 Generate Parentheses
题目描述:Generate Parentheses Given n pairs of parentheses, write a function to generate all combination ...
- LeetCode 22. 括号生成(Generate Parentheses)
22. 括号生成 22. Generate Parentheses 题目描述 给出 n 代表生成括号的对数,请你写出一个函数,使其能够生成所有可能的并且有效的括号组合. 例如,给出 n = 3,生成结 ...
- 【JAVA、C++】LeetCode 022 Generate Parentheses
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- LeetCode 笔记系列五 Generate Parentheses
题目: Given n pairs of parentheses, write a function to generate all combinations of well-formed paren ...
- leetcode第21题--Generate Parentheses
problem: Given n pairs of parentheses, write a function to generate all combinations of well-formed ...
- LeetCode(22)Generate Parentheses
题目 Given n pairs of parentheses, write a function to generate all combinations of well-formed parent ...
- 022 Generate Parentheses 生成括号
给 n 对括号,写一个函数生成所有合适的括号组合.比如,给定 n = 3,一个结果为:[ "((()))", "(()())", "(())() ...
- N-Queens And N-Queens II [LeetCode] + Generate Parentheses[LeetCode] + 回溯法
回溯法 百度百科:回溯法(探索与回溯法)是一种选优搜索法,按选优条件向前搜索,以达到目标.但当探索到某一步时,发现原先选择并不优或达不到目标,就退回一步又一次选择,这样的走不通就退回再走的技术为回溯法 ...
- leetcode022. Generate Parentheses
leetcode 022. Generate Parentheses Concise recursive C++ solution class Solution { public: vector< ...
随机推荐
- 使用tcpdump抓Android网络包
1 抓包原理 tcpdump(需Root用户运行)拦截和显示发送或收到过网络连接到该机器的TCP/IP和其他数据包.简单说就监控手机进出网络数据. 2 方法优劣 2.1优点 1.手机数据包无遗漏 2. ...
- WS_CLIPCHILDREN与WS_CLIPSIBLINGS 收藏
英文单词解释clip:夹子.子弹夹.回形针:夹住,修剪sibling:同胞兄弟或姐妹overlapped:重叠 这两个Window Stype的特性与异同素来不太清楚,今日作一笔记:MSDN的解释为: ...
- openStack 王者归来之 trivial matters
<一,openStack img 制作> tips:制作大部分cloud platforms img准备工作. <1,> http://www.pubyun.com/blog/ ...
- MySQL性能调优的方法
第一种方法 1.选取最适用的字段属性 MySQL可以很好的支持大数据量的存取,但是一般说来,数据库中的表越小,在它上面执行的查询也就会越快.因此,在创建表的时候,为了获得更好的 性能,我们可以将表中字 ...
- Apache OFbiz entity engine源代码解读
简单介绍 近期一直在看Apache OFbiz entity engine的源代码.为了能够更透彻得理解,也由于之前没有看人别人写过分析它的文章,所以决定自己来写一篇. 首先,我提出一个问题,假设你有 ...
- hdu 5040 BFS 多维化处理图
http://acm.hdu.edu.cn/showproblem.php?pid=5040 跟这一题http://blog.csdn.net/u011026968/article/details/3 ...
- SQL Server索引进阶:第十级,索引内部结构
原文地址: Stairway to SQL Server Indexes: Level 10,Index Internal Structure 本文是SQL Server索引进阶系列(Stairway ...
- C语言字符转换ASCII码
//函 数 名:CharToHex()//功能描述:把ASCII字符转换为16进制//函数说明://调用函数://全局变量://输 入:ASCII字符//返 回:16进制///////// ...
- 【转】adb.exe,start-server' failed -- run manually if necessary
[转]Android adb.exe程序启动不起来,如何处理 解决问题: 百度google大家多说的是任务管理器 kill掉adb 或者重启adb server,但我任务管理器就没有adb ,猜测是某 ...
- 关于TableViewCell高度自适应问题的整理
TableViewCell高度自适应在网上有很多资料,我只想找出最最最简单的一种方法. 首先梳理一下思路.说到TableViewCell我们第一个想到的问题或许就是cell的复用问题. 1. [se ...