22. Generate Parentheses——本质:树,DFS求解可能的path
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:
[
"((()))",
"(()())",
"(())()",
"()(())",
"()()()"
]
class Solution(object):
def generateParenthesis(self, n):
"""
:type n: int
:rtype: List[str]
[ "((()))",
"(()())",
"(())()",
"()(())",
"()()()"]
()
/ \
() ()
/ \ / \
() ()()()
"""
ans = []
path = []
self.gen_par_helper(n*2, path, ans)
return ans def is_valid_par(self, par):
stack = []
for c in par:
if c == "(":
stack.append("(")
else:
if stack:
stack.pop()
else:
return False
return len(stack) == 0 def gen_par_helper(self, n, path, ans):
if n == 0:
if self.is_valid_par(path):
ans.append("".join(path))
return
for c in "()":
path.append(c)
self.gen_par_helper(n-1, path, ans)
path.pop()
class Solution(object):
def generateParenthesis(self, n):
"""
:type n: int
:rtype: List[str]
[ "((()))",
"(()())",
"(())()",
"()(())",
"()()()"]
()
/ \
() ()
/ \ / \
() ()()()
"""
ans = []
self.gen_par_helper(n, n, "", ans)
return ans def gen_par_helper(self, left, right, path, ans):
if left == 0 and right == 0:
ans.append(path)
return
if left > 0:
self.gen_par_helper(left-1, right, path+"(", ans)
if right > 0 and right > left:
self.gen_par_helper(left, right-1, path+")", ans)
第二种更快,只是不那么容易想到!
22. Generate Parentheses——本质:树,DFS求解可能的path的更多相关文章
- [Leetcode][Python]22: Generate Parentheses
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 22: Generate Parentheseshttps://oj.leet ...
- 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,输出" ...
- 【LeetCode】22. Generate Parentheses (2 solutions)
Generate Parentheses Given n pairs of parentheses, write a function to generate all combinations of ...
- 22. Generate Parentheses (recursion algorithm)
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- leetcode@ [22]Generate Parentheses (递归 + 卡特兰数)
https://leetcode.com/problems/generate-parentheses/ Given n pairs of parentheses, write a function t ...
- 22.Generate Parentheses[M]括号生成
题目 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 parenthes ...
- 【LeetCode】22. Generate Parentheses 括号生成
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:括号, 括号生成,题解,leetcode, 力扣,Pyt ...
随机推荐
- CUBRID学习笔记 21 查看主键外键索引
命令 show create table game; game是表名 在web管理中,请在sql标签中查,不要在query中执行. show create table game; === <Re ...
- Python基础学习笔记(五)常用字符串内建函数
参考资料: 1. <Python基础教程> 2. http://www.runoob.com/python/python-strings.html 3. http://www.liaoxu ...
- 《Linux内核设计的艺术》学习笔记(四)默认段和偏移寄存器
参考书籍:<Intel微处理器> 表1 默认的16位段 + 偏移寻址组合 段 偏移量 特殊用途 CS IP 指令地址 SS SP或BP 堆栈地址 DS BX.DI.SI.8位或16位数 数 ...
- Perl5中19个最重要的文件系统工具
在写脚本处理文件系统时,经常需要加载很多模块.其中好多有用函数分散在各种不同的模块中.它们有些是Perl的内置函数,有些是在同Perl一起发行的标准模块中,另外一些是通过CPAN安装的. 下面来看15 ...
- elcipse 中利用maven创建web工程
如何创建: http://huxiaoheihei.iteye.com/blog/1766986 遇到的问题: 1: 如果spring MVC配置了 <servlet> <servl ...
- fuel3.2安装
http://software.mirantis.com/quick-start/ 准备环境用的ubuntu12.04,只需要一个网卡通外网 下载好相关的iso,img,iso放到相应的iso目录 编 ...
- 使用myeclipse建立maven项目(重要)
maven是管理项目的,myeclipse是编写代码的.第一次写项目都要配置好多东西,很麻烦,now 来看看怎样新建一个maven项目. 工具/原料 myeclipse maven 方法/步骤 ...
- Linux下搭建SVN服务
SVN有几种方式进行访问,比较常见的是通过自带协议访问(svn://),配置很简单,还有一种就是http协议访问,需要结合apache服务,配置相对繁琐. 安装svn yum -y install s ...
- 【Todo】【读书笔记】大数据Spark企业级实战版 & Scala学习
下了这本<大数据Spark企业级实战版>, 另外还有一本<Spark大数据处理:技术.应用与性能优化(全)> 先看前一篇. 根据书里的前言里面,对于阅读顺序的建议.先看最后的S ...
- Android最佳性能实践(一)——合理管理内存
有不少朋友都问过我,怎样才能写出高性能的应用程序,如何避免程序出现OOM,或者当程序内存占用过高的时候该怎么样去排查.确实,一个优秀的应用程序,不仅仅要功能完成得好,性能问题也应该处理得恰到好处.为此 ...