【LeetCode】22. Generate Parentheses (I thought I know Python...)
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...)的更多相关文章
- 【LeetCode】22. Generate Parentheses (2 solutions)
Generate Parentheses Given n pairs of parentheses, write a function to generate all combinations of ...
- 【LeetCode】22. Generate Parentheses 括号生成
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:括号, 括号生成,题解,leetcode, 力扣,Pyt ...
- 【leetcode】22. Generate Parentheses
题目描述: Given n pairs of parentheses, write a function to generate all combinations of well-formed par ...
- 【一天一道LeetCode】#22. Generate Parentheses
一天一道LeetCode (一)题目 Given n pairs of parentheses, write a function to generate all combinations of we ...
- 【LeetCode】474. Ones and Zeroes 解题报告(Python)
[LeetCode]474. Ones and Zeroes 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ ...
- 【LeetCode】113. Path Sum II 解题报告(Python)
[LeetCode]113. Path Sum II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fu ...
- 【LeetCode】299. Bulls and Cows 解题报告(Python)
[LeetCode]299. Bulls and Cows 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题 ...
- 【LeetCode】743. Network Delay Time 解题报告(Python)
[LeetCode]743. Network Delay Time 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: ht ...
- 【LeetCode】518. Coin Change 2 解题报告(Python)
[LeetCode]518. Coin Change 2 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目 ...
随机推荐
- SignalR 2.0 入门与提高
SignalR 2.0 入门与提高 SignalR 2.0 最近整理了SignalR2.0 部分知识点,原文翻译,由于自己是土鳖,翻译得不好的地方,欢迎指正!仅供各位初学者学习! 第一节. 入门ASP ...
- 你的flume-ng的第一篇博客
我在flume-ng 1.1.0 孵化版的时候就开始接触了,自己也搞了一段时间,没事扯扯心得吧. 先说在前面,flume-ng 后面的版本,我没仔细读change log ,比较农民 , 不知道新添了 ...
- Model Validation(模型验证)
Model Validation(模型验证) 前言 阅读本文之前,您也可以到Asp.Net Web API 2 系列导航进行查看 http://www.cnblogs.com/aehyok/p/344 ...
- 推荐5个漂亮的网站html源码
给大家推荐几个很漂亮的html网站源码模板,下面就简单列几个,更多的自己可以去看 1.大屏背景摄影工作室作品案例网页模板 [效果预览及下载] 2.粉色响应式IT科技服务器机房企业模板 [效果预览及下载 ...
- RSA加密解密与签名验证
关于RSACryption帮助类定义见RSACryption 一.加密与解密 //定义明文和密文变量 string plaintext = "天道酬勤,厚德载物!"; string ...
- 企业架构研究总结(28)——TOGAF架构开发方法(ADM)之需求管理阶段
1.11 需求管理(Requirements Management) 企业架构开发方法各阶段——需求管理 1.11.1 目标 本阶段的目标是定义一个过程,使企业架构的需求可以被识别.存储并与其他架构开 ...
- springMVC3学习(八)--全局的异常处理
在springMVC的配置文件中: <bean id="exceptionResolver" class="org.springframework.web.serv ...
- HtmlAgilityPack实战代码
C#采集代理服务器ip并设置IE代理--HtmlAgilityPack实战代码 今天在博客园看到一篇文章,说是C#采集某某的数据,其实做采集小软件很久了, 用的最好的还是HtmlAgilityPack ...
- DES加密解密 与 Cookie的封装(C#与js互相加密解密)
2D JS框架 - DES加密解密 与 Cookie的封装(C#与js互相加密解密) 这次实现了JS端的DES加密与解密,并且C#端也能正确解析DES的密文(反之也实现了) 使用的代码如下,非常方 ...
- JavaScript –type
JavaScript –类型之我晕 每次写博我觉得取上恬当的题目比整篇行文都难,词量有限的情况下突然想到JavaScript拾遗应该会是一个非常文艺而夺目的博文题目,但我并没有急着使用,经验告诉我应该 ...