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. Vue-router(1)之component标签

    1. 使用 <component>标签实现组件切换 <component> 是Vue提供的标签语法:有一个is属性,is的作用就是显示指定的组件 <template> ...

  2. 吴裕雄--天生自然MySQL学习笔记:MySQL 数据类型

    MySQL中定义数据字段的类型对你数据库的优化是非常重要的. MySQL支持多种类型,大致可以分为三类:数值.日期/时间和字符串(字符)类型. 数值类型 MySQL支持所有标准SQL数值数据类型. 这 ...

  3. css块级元素

    <CSS权威指南>中文字显示:任何不是块级元素的可见元素都是内联元素.其表现的特性是“行布局”形式,这里的“行布局”的意思就是说其表现形式始终以行进行显示.比如,我们设定一个内联元素bor ...

  4. IDA解析so文件异常(Binary data is incorrect maximum possible value is xx)

    错误信息 Binary data is incorrect maximum possible value is 0 错误原因 so文件损坏 或者ida换成32 解决办法 重新获得so文件,或者调整id ...

  5. Spring Cloud Alibaba 教程 | Nacos(五)

    扩展配置(extended configurations) 通过之前的学习,我们知道应用引入nacos配置中心之后默认将会加载Data ID= ${prefix} - ${spring.profile ...

  6. MyBatis学习——动态SQL

    开发人员在使用JDBC框架或者其他类似的框架进行数据库开发时,通常都要根据需求去手动拼接SQL,这样非常麻烦,而myBatis提供了对SQL语句动态组装的功能,恰好解决了这一问题. 一,动态SQL中的 ...

  7. docker---设置镜像加速器

    国内从 Docker Hub 拉取镜像有时会遇到困难,此时可以配置镜像加速器,国内很多云服务商都提供了国内加速器服务,如: Azure 中国镜像: https://dockerhub.azk8s.cn ...

  8. MySQL--mysqldump(数据导出工具)

    mysqldump 客户端工具用来备份数据库或在不同数据库之间进行数据迁移.备份内容包含创建表或装载表的 SQL 语句.mysqldump 目前是 MySQL 中最常用的备份工具. 有 3 种方式来调 ...

  9. Matlab高级教程_第一篇:Matlab基础知识提炼_05

    第九节:矩阵的操作 第十节:数组与矩阵 linspace函数

  10. Java--平台版本、跨平台、JVM、JDK、JRE

    Java2平台版本 Java2平台包括标准版(J2SE).企业版(J2EE)和微缩版(J2ME)三个版本 J2SE 包含那些构成Java语言核心的类. J2EE 包含J2SE 中的类,并且还包含用于开 ...