【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/ 题目 ...
随机推荐
- DDD实践2
DDD实践切入点(二) 承前:大型系统的支撑,应用系统开发思想的变迁,DDD实践切入点(一) 从大比例结构入手已经开始了系统的建设,大家都知道需求是会不断变化不断深入的,刚开始自然是模糊的大比例结构对 ...
- ASP.NET开发,简化与封装
ASP.NET开发,简化与封装 微软的ASP.NET的开发,就是面向对象的编程,当然前端也能体验至面向对象的话,使用Web控件也必须的. 任一控件,我们均可以在后端.aspx.cs或.aspx.vb程 ...
- SpringMVC全注解
SpringMVC全注解不是你们那么玩的 前言:忙了段时间,忙得要死要活,累了一段时间,累得死去活来. 偶尔看到很多零注解配置SpringMVC,其实没有根本的零注解. 1)工程图一张: web.xm ...
- UrlRouting的理解
UrlRouting的理解 文章内容 根据对Http Runtime和Http Pipeline的分析,我们知道一个ASP.NET应用程序可以有多个HttpModuel,但是只能有一个HttpHand ...
- 事件处理(Event Handlers) ng-click操作
事件处理(Event Handlers) ng-click操作 step 10 本文主要通过介绍ng-click方法来对angularjs中的事件处理方法做个了解. 1.切换目录 git checko ...
- JavaScript精彩范例(1)——Jquery EasyUI应用的一个框架实例
从网上看到的,非常漂亮,放在这里和大家分享一下,作者是疯狂秀才 这是截图 >>这是下载地址<<
- 【转载】matlab如何判断一个点是否在多面体内
转载自:http://www.52souji.net/point-within-a-polyhedron/ 我遇到的一个实际问题是:要在空位区域随机放置一定数量的原子,这些原子在空位区域任何一处存在的 ...
- TeamCity vs Jenkins: Which is the Better Continuous Integration (CI) Server for .NET Software Development?
原文:http://www.excella.com/insights/teamcity-vs-jenkins-better-continuous-integration-server So, you’ ...
- sqlserver中数据的四种插入方式
1.insert into stuInfo(name,stuId) values('李洁','19291727')insert into stuInfo(name,stuId) values('李康' ...
- iOS基础 - 触摸事件&手势识别
================================================================== 一.触摸事件&手势识别 1> 4个触摸事件,针对视图 ...