[Leetcode][Python]22: Generate Parentheses
# -*- coding: utf8 -*-
'''
__author__ = 'dabay.wang@gmail.com' 22: Generate Parentheses
https://oj.leetcode.com/problems/generate-parentheses/ 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:
"((()))", "(()())", "(())()", "()(())", "()()()" ===Comments by Dabay===
递归。
用left和right来记录剩余的左右括号数量。
如果都不剩余了,把结果放入要返回的列表中。
如果剩下的左括号比右括号多,说明不是合法的组合,返回。
''' class Solution:
# @param an integer
# @return a list of string
def generateParenthesis(self, n):
def generateParenthesis2(left, right, string, res):
if left == 0 and right == 0:
res.append(string)
return
if left > right:
return
if left > 0:
generateParenthesis2(left-1, right, string + "(", res)
if right > 0:
generateParenthesis2(left, right-1, string + ")", res) res = []
generateParenthesis2(n, n, "", res)
return res def main():
sol = Solution()
print sol.generateParenthesis(4) if __name__ == '__main__':
import time
start = time.clock()
main()
print "%s sec" % (time.clock() - start)
[Leetcode][Python]22: Generate Parentheses的更多相关文章
- 【一天一道LeetCode】#22. Generate Parentheses
一天一道LeetCode (一)题目 Given n pairs of parentheses, write a function to generate all combinations of we ...
- 【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 (I thought I know Python...)
I thought I know Python... Actually , I know nothing... 这个题真想让人背下来啊,每一句都很帅!!! Given n pairs of paren ...
- 【LeetCode】22. Generate Parentheses 括号生成
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:括号, 括号生成,题解,leetcode, 力扣,Pyt ...
- LeetCode OJ 22. Generate Parentheses
题目 Given n pairs of parentheses, write a function to generate all combinations of well-formed parent ...
- 【leetcode】22. Generate Parentheses
题目描述: Given n pairs of parentheses, write a function to generate all combinations of well-formed par ...
- LeetCode:22. Generate Parentheses(Medium)
1. 原题链接 https://leetcode.com/problems/generate-parentheses/description/ 2. 题目要求 给出一个正整数n,请求出由n对合法的圆括 ...
- 22. Generate Parentheses(ML)
22. Generate Parentheses . Generate Parentheses Given n pairs of parentheses, write a function to ge ...
- 刷题22. Generate Parentheses
一.题目说明 这个题目是22. Generate Parentheses,简单来说,输入一个数字n,输出n对匹配的小括号. 简单考虑了一下,n=0,输出"";n=1,输出" ...
随机推荐
- 嵌入式程序员应知道的0x10个基本问题
来源:网络 嵌入式程序员应知道的0x10个基本问题 1 . 用预处理指令#define 声明一个常数,用以表明1年中有多少秒(忽略闰年问题)#define SECONDS_PER_YEAR (60 ...
- A Byte of Python 笔记(3)运算符和表达式
第5章 运算符与表达式 大多数语句(逻辑行)都包含表达式.例子,如 2 + 3.一个表达式可以分解为运算符和操作数. 运算符 运算符 名称 说明 例子 + 加 两个对象相加 3 + 5得到8.'a' ...
- laravel5.1框架简介及安装
最近自己出来实习了,进入了一个新的环境,不仅是生活中,在代码和架构中也完全是一个新的架构.由于公司使用laravel5.1框架,所以最近学习了laravel5.1框架,好了接下来就简单介绍一下lara ...
- FastJSON应用前测试
FastJSON 应用前测试 FastJSON是一个很好的java开源json工具类库,相比其他同类的json类库,它的速度的确是fast,最快!但是文档做得不好,在应用前不得不亲测一些功能.\ 实际 ...
- C语言入门(21)——使用DBG对C语言进行调试
C语言入门(21)--使用DBG对C语言进行调试 程序中除了一目了然的Bug之外都需要一定的调试手段来分析到底错在哪.到目前为止我们的调试手段只有一种:根据程序执行时的出错现象假设错误原因,然后在代码 ...
- 字符编码知识:Unicode、UTF-8、ASCII、GB2312等编码之间是如何转换的?
转自: http://apps.hi.baidu.com/share/detail/17798660 字符编码是计算机技术的基石,想要熟练使用计算机,就必须懂得字符编码的知识.不注意的人可能对这个不 ...
- webpack入门笔记
此为第一篇主要是webpack入门笔记: http://if-true.com/2015/10/16/webpack-tutorial-translate.html
- 用Meta 取消流量器缓存实现每次访问都刷新页面方便调试
如果想禁止浏览器从本地缓存中调阅页面,可以设置网页不保存在缓存中,每次访问都刷新页面,下面是Meta在这方便的用法,需要的朋友可以参考下: <!-- 禁止浏览器从本地缓存中调阅页面.--> ...
- Yii中的CCheckBoxColumn在widget中的用法
'columns'=>array( array( 'class'=>'CCheckBoxColumn', 'id'=>'us ...
- 利用PowerDesigner15在win7系统下对MySQL 进行反向project(二)
利用PowerDesigner15在win7系统下对MySQL 进行反向project 1.打开PowerDesigner,建立新模型.选择Physical Data Model中的Physical ...