22. Generate Parentheses(ML)
22. Generate Parentheses
. Generate Parentheses Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. For example, given n = , a solution set is: [ "((()))", "(()())", "(())()", "()(())", "()()()" ]
class Solution(object):
def generateParenthesis(self, n):
"""
:type n: int
:rtype: List[str]
"""
if n == 0: return ['']
ans = []
for c in xrange(n):
for left in self.generateParenthesis(c):
for right in self.generateParenthesis(n-1-c):
ans.append('({}){}'.format(left, right))
return ans
class Solution(object):
# Brute Force
def generateParenthesis(self, n):
def generate(A=[]):
if len(A) == 2 * n:
if valid(A):
ans.append("".join(A))
else:
A.append('(')
generate(A)
A.pop()
A.append(')')
generate(A)
A.pop()
def valid(A):
bal = 0
for c in A:
if c == '(':
bal += 1
else:
bal -= 1
if bal < 0: return False
return bal == 0
ans = []
generate()
return ans
# Backtracking
def generateParenthesis2(self, N):
ans = []
def backtrack(S='', left=0, right=0):
if len(S) == 2 * N:
ans.append(S)
return
if left < N:
backtrack(S + '(', left + 1, right)
if right < left:
backtrack(S + ')', left, right + 1)
backtrack()
return ans
# Closure Number
def generateParenthesis3(self, N):
if N == 0: return ['']
ans = []
for c in range(N):
# for c in xrange(N):
for left in self.generateParenthesis(c):
for right in self.generateParenthesis(N - 1 - c):
ans.append('({}){}'.format(left, right))
return ans
sn = Solution()
print(sn.generateParenthesis(3))
print(sn.generateParenthesis2(3))
print(sn.generateParenthesis3(3))




22. Generate Parentheses(ML)的更多相关文章
- [Leetcode][Python]22: Generate Parentheses
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 22: Generate Parentheseshttps://oj.leet ...
- 刷题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 生成括号
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- LeetCode 22. Generate Parentheses
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- 22. Generate Parentheses——本质:树,DFS求解可能的path
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- Java [leetcode 22]Generate Parentheses
题目描述: Given n pairs of parentheses, write a function to generate all combinations of well-formed par ...
- leetcode@ [22]Generate Parentheses (递归 + 卡特兰数)
https://leetcode.com/problems/generate-parentheses/ Given n pairs of parentheses, write a function t ...
随机推荐
- idea中Lombok的使用
使用了lombok的注解(@Setter,@Getter,@ToString,@@RequiredArgsConstructor,@EqualsAndHashCode或@Data)之后,就不需要编写或 ...
- mybatis:数据持久层框架
mybatis是一个持久层的框架,是Apache下的顶级项目. mybatis托管到goolecode下,再后来托管到GitHub下:https://github.com/mybatis/mybati ...
- 灰度图Matlab
[转载]matlab中pcolor绘图“少画一行”的问题 本文是关于matlab pcolor函数(slice,surf函数的情况与这个类似)绘图的问题的一些解决方案,在此记录备用 经 常处理三维(或 ...
- codeforces472C
Design Tutorial: Make It Nondeterministic CodeForces - 472C A way to make a new task is to make it n ...
- kubernetes ceph-rbd挂载步骤 类型storageClass
由于kubelet本身并不支持rbd的命令,所以需要添加一个kube系统插件: 下载插件 quay.io/external_storage/rbd-provisioner 下载地址: https:// ...
- Django ORM模型
Object Relational Mapping(ORM) 一,ORM介绍 1, ORM概念 对象关系映射(Object Relational Mapping,简称ORM)模式是一种为了解决面向对象 ...
- 数据库 -- mysql记录操作
一,概括 MySQL数据操作: DML 在MySQL管理软件中,可以通过SQL语句中的DML语言来实现数据的操作,包括 使用INSERT实现数据的插入 UPDATE实现数据的更新 使用DELETE实现 ...
- UOJ370 滑稽树上滑稽果 【状压DP】
题目分析: 答案肯定是链,否则可以把枝干放到主干. 去除一直存在的位,这样0位占满时就会结束. 用$f[S]$表示0位填埋情况,每次转移是它的一个子集,我们考虑可否转移. 再用$g[S]$存储转移是否 ...
- requests简单应用
requests发送get请求 无参数的get请求: response = requests.get(url='http://ws.webxml.com.cn/WebServices/WeatherW ...
- 【转】Qt之JSON保存与读取
简述 许多游戏提供保存功能,使得玩家在游戏中的进度可以被保存,并在以后再玩的时候进行加载.保存游戏的过程通常涉及将每个游戏对象的成员变量序列化为文件.要实现这个功能,可以采取许多格式,其中之一就是 J ...