【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/ 题目 ...
随机推荐
- c#下载文件案例
public static void HttpDown(string fileName, System.Web.UI.Page p_Page,string floder) { string path ...
- 使用 ArcGIS Online和APP进行监控操作和数据采集
主要介绍的App是Operations Dashboard for ArcGIS 和 Collector for ArcGIS,它们 提高了组织内日常操作的效率.精度和响应速度. 已经购买了 ArcG ...
- C# 订单流水号生成
例如流水号格式如下:XX201604120001,2位前缀加8位日期加4位流水号 首先各种搜索出现如下解决方案 public class SerialNoHelper { /// <summar ...
- JS数量输入控件
JS数量输入控件 很早看到kissy首页 有数量输入控件,就随便看了下功能 感觉也不怎么难 所以也就试着自己也做了一个, 当然基本的功能和他们的一样,只是用了自己的编码思想来解决这么一个问题.特此给大 ...
- IP数据报是如何在网络中转发的?
首先发送方抽取目的站的网络前缀,来判断是否目的站是否位于同一网络上,如果在二者有相同的网络前缀,则,直接使用直接交付的方式, 否则,使用由路由器互连的间接交付,这要借助IP路由表,采用表驱动法,路由表 ...
- 企业架构研究总结(33)——TOGAF架构内容框架之架构制品(上)
4. 架构制品(Architectural Artifacts) 架构制品是针对某个系统或解决方案的模型描述,与架构交付物和构建块相比,架构制品既不是架构开发方法过程各阶段的合约性产物,亦不是企业中客 ...
- java 读取图片色深
问题: 想写一个小程序可读取图片的色深(bit-depth).网上有一些软件可完成这个功能,但是我想把程序做成一个可移植的插件. 本想用c写的,但实在麻烦,最后选择java,与很多方法不用自己写,速度 ...
- IE6的那些css常见bug(汇总)
IE6的那些css常见bug(汇总) 我的微博终于在前几天建立了 虽说很早之前就知道博客园这个地方 但怕自己不能坚持去写一些东西一直没有建.这几天 我做了这个决定 把我的博客建起来 每周发一些看到的, ...
- HttpStack及其实现类
HttpStack及其实现类 前两篇已经对网络请求流程已经梳理了个大概,这次我们着重看一下HttpStack和它的其实现类.我们之前在Network篇讲过它仅有一个实现类,而今天我们讲的HttpSta ...
- HashTable类模板_C++
好久没看数据结构了,今天终于要用到hash,整理一下写了个hash类模板 template<typename T> class DataType { public: T key; Data ...