[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 ...
随机推荐
- ci框架与smarty的整合
ci框架与smarty的整合 来源:未知 时间:2014-10-20 11:38 阅读数:108 作者:xbdadmin [导读] Ci 和 smarty 的完美结合 Ci 结合 sma ...
- 使用GitHub+Hexo搭建个人博客
title: CozyMo date: 2019-12-28 16:01:29 tags: 书写 前言:搭建博客要自己打代码吗? 开始动手:搭建博客的步骤 个性化:更换主题!! 写博客:初识 mark ...
- 解决Android Studio的安装问题
今天开始了android studio的下载与安装,我再官网上下载了Android studio,下载不难,运行出来可需要一定的时间,在中途中我遇到了一些问题 一:Build错误: 在我最开始下载完A ...
- linux 解压命令总结
常用Linux 命令: [转自]https://www.jianshu.com/p/ca41f32420d6 解压缩 .tar 解包:tar xvf FileName.tar 打包:tar cvf F ...
- Python笔记_第四篇_高阶编程_二次封装
1.二次封装: 二次封装其实就是对一个类或者一个方法进行二次的改造增加新的功能. 2.一个类的二次封装: 我们以一个进程为例,我们把Process这个库进行二次封装,增加一些功能,在调用. thoma ...
- jquery时钟
<script type="text/javascript"> function getDate(){ var mydate = new Date(); //时间对象 ...
- uploadifive使用笔记
官网地址:http://www.uploadify.com/ uploadifive 是基于H5开发,所以支持移动端和PC端 <input type="file" name= ...
- 计蒜客 方程的解数(DFS)
问题描述 输出格式 输出一行,输出一个整数,表示方程的整数解的个数. 样例输入 - 样例输出 #include <stdio.h> #include <string.h> #i ...
- selector的使用,android:clickable="true"
<ImageView android:id="@+id/patrol_buzzer_btn" android:layout_width="80dp" an ...
- oracle sql语句学习(一)
oraclexe 11.0.2.0 输出到文件 SQL>spool /*完整路径*/; SQL>spool off; 多表自然链接 select spj.sno from spj join ...