I thought I know Python...

Actually , I know nothing...

这个题真想让人背下来啊,每一句都很帅!!!

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

For example, given n = 3, a solution set is:

[
"((()))",
"(()())",
"(())()",
"()(())",
"()()()"
]

题意:找出n个括号的所有合法组合

在discuss区看到一个碾压级别的解法

https://discuss.leetcode.com/topic/17510/4-7-lines-python/13

 class Solution(object):
def generateParenthesis(self, n):
"""
:type n: int
:rtype: List[str]
"""
def g(p,left,right,res=[]):
if left:
g(p+'(',left-1,right)
if right>left:
g(p+')',left,right-1)
if not right:
res += p, #相当于res.append(p)
return res
return g('',n,n)

作者说他在这个题中用了几个tricks,我试着找了一下:

line15: 用return 语句启动内层嵌套的函数

line7:   res=[]给参数传递可变类型,为内层函数分配所有g函数副本可用的变量名

line13: 最后的逗号用的太神了,没有逗号的话结果完全错误,用逗号把p变为元组

【LeetCode】22. Generate Parentheses (I thought I know Python...)的更多相关文章

  1. 【LeetCode】22. Generate Parentheses (2 solutions)

    Generate Parentheses Given n pairs of parentheses, write a function to generate all combinations of ...

  2. 【LeetCode】22. Generate Parentheses 括号生成

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:括号, 括号生成,题解,leetcode, 力扣,Pyt ...

  3. 【leetcode】22. Generate Parentheses

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

  4. 【一天一道LeetCode】#22. Generate Parentheses

    一天一道LeetCode (一)题目 Given n pairs of parentheses, write a function to generate all combinations of we ...

  5. 【LeetCode】474. Ones and Zeroes 解题报告(Python)

    [LeetCode]474. Ones and Zeroes 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ ...

  6. 【LeetCode】113. Path Sum II 解题报告(Python)

    [LeetCode]113. Path Sum II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fu ...

  7. 【LeetCode】299. Bulls and Cows 解题报告(Python)

    [LeetCode]299. Bulls and Cows 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题 ...

  8. 【LeetCode】743. Network Delay Time 解题报告(Python)

    [LeetCode]743. Network Delay Time 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: ht ...

  9. 【LeetCode】518. Coin Change 2 解题报告(Python)

    [LeetCode]518. Coin Change 2 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目 ...

随机推荐

  1. C# 通讯网关开发

    C# 通讯网关开发 楼主从12年毕业大部分时间一直从事于通讯网关的开发,刚刚学那会连C#是啥都不知道,就直接入手网关开发,前前后后到现在也算是弄了5.6个通讯协议,后来看到北风之神的socket框架和 ...

  2. CanVas类(绘图类)

    知识点: Canvas():创建一个空的画布,可以使用setBitmap()方法来设置具体的画布 Canvas(Bitmap bitmap):以bitmap对象创建一个画布,则将内容都绘制在Bitma ...

  3. 创建自己的github代码库

    前提: 1.安装git 2.在github上创建一个repository(库),如名字叫Hello-World,则git地址为https://github.com/username/Hello-Wor ...

  4. Scrum与高效能人士

    Scrum与高效能人士的执行4原则   分享了高效能人士的执行4原则,发现它和Scrum非常相近,可以形成互补. Scrum框架: 高效能人士的执行4原则框架: Scrum与4原则 Sprint Ba ...

  5. Json反序列化

    迟来的Json反序列化   源码发布 搞了一个下午,终于搞定改了这个号称中国的github...以后源码直接在这里发布了(英文实在太烂了) https://code.csdn.net/jy023050 ...

  6. 限制某个进程只能在某个CPU上运行

    首先可以调用GetSystemInfo查看有多少个CPU,再通过调用: BOOL WINAPI SetProcessAffinityMask( __in HANDLE hProcess, __in D ...

  7. 简单实现TCP下的大文件高效传输

    简单实现TCP下的大文件高效传输 在TCP下进行大文件传输不象小文件那样直接打包个BUFFER发送出去,因为文件比较大所以不可能把文件读到一个BUFFER发送出去.主要有些文件的大小可能是1G,2G或 ...

  8. Pyscripter是python下一个非常流行的开源IDE

    Pyscripter 不能正确调用另一文件中模块的问题的解析(Internal Engine 和 Remote Engine) 背景 Pyscripter是python下一个非常流行的开源IDE,笔者 ...

  9. 提高你的Java代码质量吧:不要让类型默默转换

    一.分析  在Java运算中的类型转换,是先运算在进行类型转换的.具体场景如下. 二.场景  在如下程序中: public class Client{ public static final int ...

  10. Hadoop Streaming Command Details and Q&A

    Hadoop Streaming Hadoopstreaming is a utility that comes with the Hadoop distribution. The utilityal ...