思路:

递归解决,如果左括号个数小于右括号或者左括号数小于总括号对数,则生成一个左括号,如果左括号数大于右括号,生成一个右括号。

class Solution {
public:
vector<string> ans;
int num;
void brackets(string res, int left, int right, int sum)
{
if(sum == * num) {
ans.push_back(res);
}
else{
if(left < right || left < num)brackets(res + '(', left + , right, sum + );
if(left > right)brackets(res + ')', left, right + , sum + );
}
} vector<string> generateParenthesis(int n) {
if(n == ) return ans;
num = n;
brackets("", , , );
return ans;
}
};

leetcode个人题解——#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. [LeetCode] 22. Generate Parentheses 生成括号

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

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

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

  8. leetcode@ [22]Generate Parentheses (递归 + 卡特兰数)

    https://leetcode.com/problems/generate-parentheses/ Given n pairs of parentheses, write a function t ...

  9. 【LeetCode】22. Generate Parentheses (I thought I know Python...)

    I thought I know Python... Actually , I know nothing... 这个题真想让人背下来啊,每一句都很帅!!! Given n pairs of paren ...

随机推荐

  1. mysql数据库迁移到oracle数据库后 如何删除相同的数据

    mysql数据库迁移到oracle数据库后 如何删除相同的数据 首先搞清楚有多少数据是重复的 select pid from product group by pid having count(pid ...

  2. iOS11.2-11.3.1进行越狱及问题

    设备环境:Electra.iOS11.13.1 PS:Electra最新版本进行越狱只支持11.14以下的版本.同时这是不完美越狱,每次重启手机都需要重新越狱,最后,由于Electra版本推出仓促,一 ...

  3. 阿里云centOS7.4上MySql5.6安装

    最近一个项目要部署在阿里云上,为了开发团队方便,我自费买了个ECS,先装个数据库给开发用. 因为之前都是在真机安装,与这次阿里云上的部署比起来,还是有点区别的. Mysql 1 安装mysql版本包 ...

  4. 关于“CheckBox”通过表单提交的问题

    大多数时候CheckBox取值传到java后台都是通过js取值,ajax传值,今天改一离职同事的老代码,那家伙通过表单提交一些列的CheckBox设置,没想到的是后台死活接收不正常,name.valu ...

  5. CentOS 7.x下升级Python版本到3.x系列(新老版本共存)

    由于python官方已宣布2.x系列即将停止支持,为了向前看,我们升级系统的python版本为3.x系列服务器系统为当前最新的CentOS 7.4 1.安装前查看当前系统下的python版本号 # p ...

  6. Delphi7 GDI+学习

    Delphi7自带的绘图有锯齿,所以要学习GDI+ 主要是从这个网站学习 http://www.bianceng.com/Programming/Delphi/201212/34691.htm 相关控 ...

  7. centos7.3 vsftpd 多用户配置

    1. 安装vsftpd及pam认证服务软件 yum install vsftpd* -y yum install pam* libdb-utils libdb* --skip-broken -y #设 ...

  8. python在lxml中使用XPath语法进行#数据解析

    在lxml中使用XPath语法: 获取所有li标签: from lxml import etree html = etree.parse('hello.html') print type(html) ...

  9. C 二维数组,以及自定义二维数组

    C 二维数组,以及自定义二维数组 我们通常情况下是这样定义一个二维数组的: int a[10][15]; 我们分别查看一下a,a[0],*a 都是一样的值吧 我们可以这么理解: a是一个数组的数组 a ...

  10. 引用ZXing生成二维码

    1.生成二维码 ZXing是一个开放源码的,用Java实现的多种格式的1D/2D条码图像处理库,它包含了联系到其他语言的端口. Zxing可以实现使用手机的内置的摄像头完成条形码的扫描及解码.本文引用 ...