[LC] 22. Generate Parentheses
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的更多相关文章
- [Leetcode][Python]22: Generate Parentheses
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 22: Generate Parentheseshttps://oj.leet ...
- 22. Generate Parentheses(ML)
22. Generate Parentheses . Generate Parentheses Given n pairs of parentheses, write a function to ge ...
- 刷题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 ...
- 22. Generate Parentheses产生所有匹配括号的方案
[抄题]: Given n pairs of parentheses, write a function to generate all combinations of well-formed par ...
- [LeetCode] 22. Generate Parentheses 生成括号
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- 【LeetCode】22. Generate Parentheses 括号生成
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:括号, 括号生成,题解,leetcode, 力扣,Pyt ...
- LeetCode 22. Generate Parentheses
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
随机推荐
- selenium破解人人登陆验证码
from selenium import webdriverfrom PIL import Imagefrom chaojiying import Chaojiying_Clientimport ti ...
- Windows如何设置指定的IP走专线?
很多时候在工作中难免有多重网络环境的情况,为了方便之间的互访,可能会用的VPN等虚拟专线,作为网络管理员,route命令是必会的基础技能. 我们一般连接到专线vpn以后,默认会启用远程网关,这样我们所 ...
- A - Period(kmp的next数组的应用)
For each prefix of a given string S with N characters (each character has an ASCII code between 97 a ...
- 题解 P6098 【[USACO19FEB]Cow Land G】
震惊,蒟蒻学树剖第二天就打题解 所以说,理解之后树剖这种东西其实难度真心不大.至少这种模板题都可以秒切的 这里推荐一个博客: 树剖详解 蒟蒻就是在这个博客上学到的 如果想看我自己写的总结,请点 我的博 ...
- JDK的3个bug啊,你get到了吗?
1.Annotation引用非空enum数组返回空数组 首次发现时的环境:JDK 1.8 首次发现所在项目:APIJSON 测试用例: publicenumRequestRole {/**未登录,不明 ...
- [RoarCTF 2019]Easy Calc-协议层攻击之HTTP请求走私
0X01:什么是HTTP请求走私 HTTP请求走私属于协议层攻击,是服务器漏洞的一种. HTTP请求走私是一种干扰网站处理从一个或多个用户接收的HTTP请求序列的方式的技术.使攻击者可以绕过安全控制, ...
- MySQL--InnoDB 关键特性
插入缓冲 Insert Buffer 对于非聚集索引的插入或更新操作,不是每一次直接插入到索引页中,而是先判断插入的非聚集索引页是否在缓冲池中,若在,则直接插入:若不在,则先放入到一个 Insert ...
- JAVA 算法练习(三)
拆解排序问题 后缀子串排序 题目: 对于一个字符串,将其后缀子串进行排序,例如grain 其子串有: grain rain ain in n 然后对各子串按字典顺序排序,即: ain,grain,in ...
- Consul集群版容器化部署与应用集成
背景 由于公司目前的主要产品使用的注册中心是consul,consul需要用集群来保证高可用,传统的方式(Nginx/HAProxy)会有单点故障问题,为了解决该问题,我开始研究如何只依赖consul ...
- 14 微服务电商【黑马乐优商城】:day01-springboot(理论篇)
本项目的笔记和资料的Download,请点击这一句话自行获取. day01-springboot(理论篇) :day01-springboot(实践篇) :day01-springboot(Thyme ...