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 ...
随机推荐
- css last
- vuex2.0 基本使用(1) --- state
Vuex 的核心是 store, 它是一个通过 Vuex.Store 构造函数生成的对象.为什么它会是核心呢?因为我们调用这个构造函数创建store 对象的时候,给它传递参数中包装了state, mu ...
- 牛客寒假算法训练1 D 欧拉(容斥)
1 #include<bits/stdc++.h> using namespace std; ; typedef long long ll; int p[maxn],a[maxn]; ll ...
- BZOJ2809 dispatching 【可并堆】
题目分析: yy一下就知道了,合并用可并堆少个log. 代码: #include<bits/stdc++.h> using namespace std; ; int n,m; int b[ ...
- 【XSY2733】Disembrangle DP
题目描述 有一个\(3\times n\)的网格,一些格子里已经有棋子了,一些格子里还没有. 每次你可以选择往一个没有棋子的格子里放一个棋子,但要满足这个格子上下两个格子都有棋子或左右两个格子都有棋子 ...
- 【XSY1294】sub 树链剖分
题目描述 给你一棵\(n\)个点的无根树,节点\(i\)有权值\(v_i\).现在有\(m\)次操作,操作有如下两种: \(1~x~y\):把\(v_x\)改成\(y\). \(2\):选择一个连通块 ...
- MT【292】任意存在求最值
已知向量$\textbf{a},\textbf{b}$满足:$|\textbf{a}|=|\textbf{b}|=1,\textbf{a}\cdot\textbf{b}=\dfrac{1}{2},\t ...
- Elasticsearch 安全篇
为什么要开放外网访问Elasticsearch:1.一些插件监听使用,方便及时了解线上数据的情况,比如说head插件等2.为了HTTP直接访问,有的同学考虑到通过后台TCP查询,然后返回数据,还不如直 ...
- HNOI2019总结
HNOI2019总结 Day 1 开场看三道题,T1是个计算几何,T2是个操作树加\(border\),T3题意有点复杂.想T1想了半个多小时,发现那个钝角不是很会处理,但是40分暴力应该还是可以写, ...
- Hdoj 1905.Pseudoprime numbers 题解
Problem Description Fermat's theorem states that for any prime number p and for any integer a > 1 ...