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 ...
随机推荐
- python_way,day4 内置函数(callable,chr,随机验证码,ord),装饰器
python_way,day4 1.内置函数 - 下 制作一个随机验证码 2.装饰器 1.内置函数 - 下 callable() #对象能否被调用 chr() #10进制数字对应的ascii码表中的内 ...
- Blend操作入门: 别站在门外偷看,快进来吧!(转)
来源:http://www.cnblogs.com/hielvis/archive/2010/10/21/1857415.html 有的人认为,Blend主要是用来修改一下颜色,调整一下布局之类的,大 ...
- 51nod 1413 权势二进制 背包dp
1413 权势二进制 题目来源: CodeForces 基准时间限制:1 秒 空间限制:131072 KB 一个十进制整数被叫做权势二进制,当他的十进制表示的时候只由0或1组成.例如0,1,101, ...
- POJ1011 (DFS+剪枝)
Sticks Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 129606 Accepted: 30388 Descrip ...
- jquery动画遮罩
以前一直以为遮罩都是鼠标移上去,改变透明度实现的,后来看到过这样的一个遮罩动画,然后今天自己写了一个,因为弹出的遮罩是圆形的,所以从美观上来说,这个遮罩效果更适合于方形图片. <div clas ...
- Spring读书笔记-----Spring的Bean之Bean的基本概念
从前面我们知道Spring其实就是一个大型的工厂,而Spring容器中的Bean就是该工厂的产品.对于Spring容器能够生产那些产品,则取决于配置文件中配置. 对于我们而言,我们使用Spring框架 ...
- 在Spring Data JPA 中使用Update Query更新实体类
对于 Spring Data JPA 使用的时间不长,只有两年时间.但是踩过坑的却不少. 使用下列代码 @Modifying @Query("update User u set u.firs ...
- datagrid实现行的上移和下移
<html> <head> <meta http-equiv="Content-Type" content="text/html; char ...
- C# 中的弱引用 WeakReference
C#中的弱引用(WeakReference) 我们平常用的都是对象的强引用,如果有强引用存在,GC是不会回收对象的.我们能不能同时保持对对象的引用,而又可以让GC需要的时候回收这个对象呢?.NET ...
- phalcon: 视图分层渲染,或包含其他页面
一:视图分层显现: 比如:在一个页面中,头部.底部固定不变,只有中间部分随操作变换显示.那么可以将中间部分切出来,剩余部分用作固定框架,放入:app/views/layouts目录中,起名为:base ...