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:

[
"((()))",
"(()())",
"(())()",
"()(())",
"()()()"
] Time: O(2^N)
Space: O(N)
 class Solution:
def generateParenthesis(self, n: int) -> List[str]:
res = []
self.helper(0, 0, n, '', res)
return res def helper(self, left, right, n, word, res):
if left == n and right == n:
res.append(word)
if left < n:
word += '('
self.helper(left + 1, right, n, word, res)
word = word[:-1]
if left > right:
word += ')'
self.helper(left, right + 1, n, word, res)
word = word[:-1]

Similar Solution 2:

class Solution:
def generateParenthesis(self, n: int) -> List[str]:
res = []
cur = [''] * 2 * n
self.helper(0, 0, 0, n, cur, res)
return res def helper(self, left, right, level, n, cur, res):
if left == n and right == n:
word = ''.join(cur)
res.append(word)
if left < n:
cur[level] = '('
self.helper(left + 1, right, level + 1, n, cur, res) if left > right:
cur[level] = ')'
self.helper(left, right + 1, level + 1, n, cur, res)

[LC] 22. Generate Parentheses的更多相关文章

  1. [Leetcode][Python]22: Generate Parentheses

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 22: Generate Parentheseshttps://oj.leet ...

  2. 22. Generate Parentheses(ML)

    22. Generate Parentheses . Generate Parentheses Given n pairs of parentheses, write a function to ge ...

  3. 刷题22. Generate Parentheses

    一.题目说明 这个题目是22. Generate Parentheses,简单来说,输入一个数字n,输出n对匹配的小括号. 简单考虑了一下,n=0,输出"";n=1,输出" ...

  4. 【LeetCode】22. Generate Parentheses (2 solutions)

    Generate Parentheses Given n pairs of parentheses, write a function to generate all combinations of ...

  5. 22. Generate Parentheses (recursion algorithm)

    Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...

  6. 22. Generate Parentheses产生所有匹配括号的方案

    [抄题]: Given n pairs of parentheses, write a function to generate all combinations of well-formed par ...

  7. [LeetCode] 22. Generate Parentheses 生成括号

    Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...

  8. 【LeetCode】22. Generate Parentheses 括号生成

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:括号, 括号生成,题解,leetcode, 力扣,Pyt ...

  9. LeetCode 22. Generate Parentheses

    Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...

随机推荐

  1. 【十日冲刺计划】第一日 星遇Sprint1计划会议成果

    ——小组成员 赵剑峰 张傲 周龙海 Sprint 计划会议1:定出 Sprint 目标和既定产品Backlog. 会议进程(1小时) • 首先对sprint目标进行总体介绍,概括星遇的backlog, ...

  2. D语言-运算符

    Part 0:概念 表达式:表达式是由非赋值运算符或特殊运算符和值组成的,每个表达式都可以计算出一个值 Part 1:非赋值运算符 §1.1 基本的运算符 基本的运算符有+,-,*,/,% 我相信你除 ...

  3. SQL基础教程(第2版)第6章 函数、谓词、CASE表达式:6-2 谓词

    ● 谓词就是返回值为真值的函数. ● 可以将子查询作为IN和EXISTS的参数. 本节将会和大家一起学习 SQL 的抽出条件中不可或缺的工具——谓词(predicate).例如, =. <. & ...

  4. git commit 后 尚未push到远程,撤销commit

    执行commit后,还没执行push时,想要撤销这次的commit,该怎么办? 解决方案: 使用命令: git reset --soft HEAD^ 这样就成功撤销了commit,如果想要连着add也 ...

  5. MySQL空洞问题解决

    原因:Mysql对于BLOB/TEXT值在执行大量删除操作时可能会引起空洞.空洞就是数据虽然删除了,但是依然占用服务器物理空间,会导致性能底下. 解决办法:定期使用OPTIMIZE TABLE进行碎片 ...

  6. Python说文解字_看起来有点儿像字典的元组(命名元祖)

    1. 需要一个库 namedtuple: 所谓命名元组就是对于元组的每一个元素进行起名,看起来很像访问字典一样. 实例 from collections import namedtuple Stock ...

  7. 吴裕雄--天生自然深度学习TensorBoard可视化:改造后的mnist_train

    import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data INPUT_NODE = 784 ...

  8. one_day_one_linuxCmd---scp命令

    <坚持每天学习一个 linux 命令,今天我们来学习 scp 命令> scp 命令主要用在不同的 linux 系统之间 copy 文件,基于 ssh 登录,是一种安全的复制 scp 命令的 ...

  9. sklearn KMeans聚类算法(总结)

    基本原理 Kmeans是无监督学习的代表,没有所谓的Y.主要目的是分类,分类的依据就是样本之间的距离.比如要分为K类.步骤是: 随机选取K个点. 计算每个点到K个质心的距离,分成K个簇. 计算K个簇样 ...

  10. js分页的一些思考

    一两年之前在写java的时候,分页的逻辑是写在后端的,用java去实现,jsp是动态展示页码和数据.但是对于一个用ajax加载的分页数据来说,分页的逻辑必须也在前端完成,那么就不得不去思考一下在js里 ...