[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< ...
随机推荐
- 每天学点Linux:二
关于输入输出和重定向: 默认情况下标准I/O的输入为键盘,输出为显示屏.输入和输出可以通过符号‘>’进行重定向. 例如可以通过命令:$>newfile 来创建一个新文件(如果newfile ...
- MyEclipse13中修改Servlet.java源代码
Servlet.java源代码想要修改的步骤,与低版本的不同废话少说,直接来步骤: 1,在myEclipse的安装目录中搜索com.genuitec.eclipse.wizards文件,如图:选择co ...
- To the Max(矩阵压缩)
To the Max Time Limit : 2000/1000ms (Java/Other) Memory Limit : 20000/10000K (Java/Other) Total Su ...
- Android 中万能的 BaseAdapter(Spinner,ListView,GridView) 的使用!
大家好!今天给大家讲解一下BaseAdapter(基础适配器)的用法,适配器的作用主要是用来给诸如(Spinner,ListView,GridView)来填充数据的.而(Spinner,ListVie ...
- xcode 不值钱的动画UIButton
#import "ViewController.h" @interface ViewController () /** 按钮 */ @property(nonatomic,weak ...
- 安卓中onBackPressed ()方法的使用
一.onBackPressed()方法的解释 这个方法放在 void android.app.Activity.onBackPressed() 在安卓API中它是这样解释的: public void ...
- AsyncSocket的长连接使用
使用背景:需要跟服务器长期保持连接进行即时通讯:还有在跟智能硬件建立实时链接进行同步智能硬件的状态等,最近我就做项目就碰到需要实时更新智能硬件的状态(比如智能硬件的电量,以及其它工作状态),跟智能硬件 ...
- ExtJs007最常用的查询方法
Ext.onReady(function () { Ext.create('Ext.panel.Panel', { title: '我的面板', width: '100%', height: 400, ...
- ExtJs目录说明
Ext开发包目录结构说明builds目录为ExtJS压缩后的代码docs目录为ExtJS的文档examples目录中是官方的演示示例locale是多国语言的资源文件, 其中ext - lang - z ...
- mac的svn之cornerstone简易教程
链接地址:http://jingyan.baidu.com/article/9989c74612a55af648ecfef2.html 背景: 关于cornerstone的介绍很少: 这里介绍mac的 ...