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 ...
随机推荐
- git 提交的步骤
1. git init //初始化仓库 2. git add .(文件name) //添加文件到本地仓库 3. git commit -m "first commit" / ...
- Opencv画图操作
1. 画矩形 MyRect rect;rect.left = 5;rect.top = 5;rect.right = 100;rect.bottom = 100;IplImage * pColorIm ...
- Simple Use IEnumerable<T>
Private static IEnumerable<T> FunDemo(T para) { while(...) { .... yield return Obj; } } static ...
- hdu-3294(最长回文子串)
题意:给你一个字符和一个字符串让你求出最长回文子串并且输出来,答案需要根据给出的字符转换一下,就是将给出的字符认定为a,然后依次向后推: 解题思路:manacher模板+一些处理 代码: #inclu ...
- shelve 模块
shelve 模块概述: shelve是python的自带model. 可以直接通过import shelve来引用. shelve类似于一个存储持久化对象的持久化字典,即字典文件. ...
- 青蛙的约会 POJ - 1061 (exgcd)
两只青蛙在网上相识了,它们聊得很开心,于是觉得很有必要见一面.它们很高兴地发现它们住在同一条纬度线上,于是它们约定各自朝西跳,直到碰面为止.可是它们出发之前忘记了一件很重要的事情,既没有问清楚对方的特 ...
- 如何判断是否为同一个App,Ionic3如何修改包名
如何判断是否同一个App 使用Ionic3创建了两个项目demo1.demo2,然后使用同一个JDK,生成了两个不同的keystore证书. 结果在手机端安装的时候,先安装demo1,没有任何替换的提 ...
- os x && linux 文件传输基础命令
一.从服务器下载文件到本机 1.修改文件所属 由于只能下载文件所属为自己的文件,所以要做修改文件所属的操作. chown hudelei /opt/logs/tomcat/app/tomcat_stk ...
- 用webpack2.0构建vue2.0单文件组件超级详细精简实例
npm init -y 初始化项目 //-y 为自动生成package.json,如果需要自行配置,去掉-y即可 安装各种依赖项 npm install --save vue 安装vue2.0 np ...
- Android 2019最新面试实战总结
Android: 今日头条屏幕适配的原理? 1:首先计算出 density,计算公式:当前设备屏幕总宽度(单位为像素)/ 设计图总宽度(单位为 dp) = densitydensity 的意思就是 1 ...