[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,输出" ...
随机推荐
- Android Studio中如何创建AIDL
实现客户端添加Book,Service接收并打印出书籍信息 一.创建Book类 1.创建文件夹(本文命名为aidl) 2.创建Book类并继承Parcelable接口(原因:AIDL只能传送继承Par ...
- 项目与软件推荐之编辑器-QOwnNotes(刺激自己)
项目与软件推荐之编辑器-QOwnNotes 今天推荐一款软件 QOwnNotes,是一款普通文本笔记软件.以某个路径为目录,罗列出目录下所有的 md 文件或者 txt 文件. 有如下亮点: 启动速度快 ...
- jQuery 中的 Ajax $.ajax() load() $.get() $.post() $.getJSON() $.getScript()
1. $.ajax()方法 参数对象属性如下: 参数名 类型 描述 url String (默认: 当前页地址) 发送请求的地址. type String (默认: "GET") ...
- linear encoder 和 PCA
- poj 3020 Antenna Placement (最小路径覆盖)
链接:poj 3020 题意:一个矩形中,有n个城市'*'.'o'表示空地,如今这n个城市都要覆盖无线,若放置一个基站, 那么它至多能够覆盖本身和相邻的一个城市,求至少放置多少个基站才干使得全部的城市 ...
- [Oracle] - 性能优化工具(3) - ADDM
ADDM 通过检查和分析AWR获取的数据来推断Oracle数据库中可能的问题.并给出优化建议. 获取ADDM的方法例如以下: @?/rdbms/admin/addmrpt.sql 以下能够看一个样例: ...
- checkbox和radio的样式美化问题
如果你下定决心要改变现有的默认的checkbox和radio的样式,那么我目前有两种办法: 1.自己动手写一个,也就是自己写代码实现将input的checkbox和radio默认的样式隐藏掉,使用绝对 ...
- 【邻接表字符串Hash】【HDU1800】Flying to the Mars
题意: 给你N个数字,带前导0,问出现最多的数字个数 思路: 读入,清楚前导0,Hash. 用邻接表字符串Hash有一下几点注意 string,不要memset,否则地址也没了,涉及到stl的东西,少 ...
- JAVA责任链设计模式
<JAVA与模式>之责任链模式 在阎宏博士的<JAVA与模式>一书中开头是这样描述责任链(Chain of Responsibility)模式的: 责任链模式是一种对象的行为模 ...
- 前端新人学习笔记-------html/css/js基础知识点(三)
这断时间家里有点事,上班也有点任务,所以几天没看视频没来更新了.今天来更新一下了. 一:默认样式重置 但凡是浏览默认的样式,都不要使用. body,p,h1,h2,h3,h4,h5,h6,dl,dd{ ...