# -*- 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的更多相关文章

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

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

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

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

  3. 【LeetCode】22. Generate Parentheses (I thought I know Python...)

    I thought I know Python... Actually , I know nothing... 这个题真想让人背下来啊,每一句都很帅!!! Given n pairs of paren ...

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

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

  5. LeetCode OJ 22. Generate Parentheses

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

  6. 【leetcode】22. Generate Parentheses

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

  7. LeetCode:22. Generate Parentheses(Medium)

    1. 原题链接 https://leetcode.com/problems/generate-parentheses/description/ 2. 题目要求 给出一个正整数n,请求出由n对合法的圆括 ...

  8. 22. Generate Parentheses(ML)

    22. Generate Parentheses . Generate Parentheses Given n pairs of parentheses, write a function to ge ...

  9. 刷题22. Generate Parentheses

    一.题目说明 这个题目是22. Generate Parentheses,简单来说,输入一个数字n,输出n对匹配的小括号. 简单考虑了一下,n=0,输出"";n=1,输出" ...

随机推荐

  1. HTML 5 学习之应用程序缓存

    什么是应用程序缓存(Application Cache)? HTML5 引入了应用程序缓存,这意味着 web 应用可进行缓存,并可在没有因特网连接时进行访问. 应用程序缓存为应用带来三个优势: 离线浏 ...

  2. C++ Primer第四版 15.9 再谈文本查询 程序实现

    编程过程中发现书本中的示例程序并不完全,某些地方存在错误,现已改正并添加少许注释.. 1 #include<iostream> 2 #include<fstream> #inc ...

  3. Selenium2Library使用Remote功能(转载并更新)

    在selenium2library库的open browser中,除了我们常用的url,browser外,还有几个不常用的参数.如:remote_url的用法 1.下载selenium-server- ...

  4. Oracle EBS-SQL (WIP-7):检查当月任务发放记录.sql

    select        WE.DESCRIPTION                                                任务说明,        DECODE(WE.S ...

  5. 如何在Root的手机上开启ViewServer,使得HierachyViewer能够连接

    前期准备: 关于什么是Hierarchy Viewer,请查看官方文档:http://developer.android.com/tools/debugging/debugging-ui.html.个 ...

  6. poj3100---求根问题

    题意:a的n方=b,a这个整数与b开n方的值相近,分别向上取整和向下取整,同时n方,b一定介于这两个整数之间,然后比较这两个数与b的距离,取最近的 收获:c++的cei和floor函数在c中的向上取整 ...

  7. poj--1517

    n从0取到9,一个for循环,n++,n=0,n<=9 n=4,求1/4!+1/3!+1/2!+1/1!+1/0! 需要一个for循环,i=n,i>=0,i-- sum+=factoria ...

  8. Python学习笔记5-字符串、bool、数值操作和数组字典排序

    1.字符串 # 字符串数字之间转换 # x = int("6") # print type(x) #<type 'str'> # y = str(6) # print ...

  9. wx.Dialog

    wx.Dialog A dialog box is a window with a title bar and sometimes a system menu, which can be moved ...

  10. jquery实现隐藏显示层动画效果、仿新浪字符动态输入、tab效果

    已经有两年多没登陆csdn账号了,中间做了些旁的事,可是现在却还是回归程序,但改做前端了,虽然很多东西都已忘得差不多了,但还是应该摆正心态,慢慢来,在前端漫游,做一只快乐双鱼. 路是一步一步走出来的, ...