题目:

解法:

The idea is intuitive. Use two integers to count the remaining left parenthesis (n) and the right parenthesis (m) to be added. At each function call add a left parenthesis if n >0 and add a right parenthesis if m>0. Append the result and terminate recursive calls when both m and n are zero.

class Solution
{
public:vector<string> generateParenthesis(int n) {vector<string> res;addingpar(res, "", n, 0);return res;}void addingpar(vector<string> &v, string str, int n, int m){if(n==0 && m==0) {v.push_back(str);return;}if(m > 0){ addingpar(v, str+")", n, m-1); }if(n > 0){ addingpar(v, str+"(", n-1, m+1); }}
};

LeetCode22——Generate Parentheses(给定n对括号,然后看有多少正确的括号组合)相关推荐

  1. [CareerCup] 9.6 Generate Parentheses 生成括号

    9.6 Implement an algorithm to print all valid (e.g., properly opened and closed) combinations of n-p ...

  2. 递归/回溯:Generate Parentheses生成合法括号

    已知n组括号,开发一个程序,生成这n组括号所有的合法的组合可能. 例如:n = 3 结果为: ["((()))", "(()())", "(())() ...

  3. LeetCode 22. 括号生成(Generate Parentheses)

    题目描述 给出 n 代表生成括号的对数,请你写出一个函数,使其能够生成所有可能的并且有效的括号组合. 例如,给出 n =3,生成结果为: ["((()))","(()() ...

  4. LeetCode算法入门- Generate Parentheses -day16

    LeetCode算法入门- Generate Parentheses -day16 题目描述 Given n pairs of parentheses, write a function to gen ...

  5. [leetcode] 22. Generate Parentheses

    题目大意 https://leetcode.com/problems/generate-parentheses/description/ 22. Generate Parentheses Given ...

  6. [LeetCode] Generate Parentheses

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

  7. LeetCode:Generate Parentheses

    题目链接 Given n pairs of parentheses, write a function to generate all combinations of well-formed pare ...

  8. LeetCode 22. Generate Parentheses

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

  9. 36/100. Generate Parentheses

    给定n对括号,要求输出所有合法的括号排列. 使用DFS算法,主要分为两种情况: ①若有剩余的"(",则添加"("后继续递归: ②若有剩余的")&quo ...

最新文章

  1. Python2 与 Python3 共存,pip2 与 pip3 共存切换设置
  2. Visual Studio2008的微软官方下载地址
  3. Python标准库01 正则表达式(re包)
  4. 【Android 高性能音频】OboeTester 音频性能测试应用 ( Oboe 输出测试参数 | API 选择 | 音频输出设备选择 | 采样率 | 通道 | 采样格式 | 播放偏好 )
  5. 【Python】itertools之product函数
  6. MapReduce-计数器
  7. HDU 2444 The Accomodation of Students 二分图匹配
  8. 2016中国信息通信大数据大会召开在即,精彩看点提前揭晓
  9. 网赚新手必读:Google Adsense五条法则
  10. 清除定时器 和 vue 中遇到的定时器setTimeout setInterval问题
  11. vb查询mysql数据库_怎么在vb程序中查找数据库信息并显示
  12. HMC5883L 电子指南针用树莓派进行磁场干扰过滤 校准
  13. 快乐机:人活着真的只为追求快乐吗?
  14. Android中通过USB连接来控制硬件设备
  15. Vue学习(一)从 mvx模式 到 mvvm模式
  16. 你不知道的网络招聘与求职潜规则
  17. Mini CFA 考试练习题 Macroeconomics
  18. CocosDashboard课堂笔记
  19. 《智能商业》由阿里巴巴学术委员会主席、前总参谋长曾鸣亲自编写,值得一读!
  20. 关于下载pdf请求不到数据问题

热门文章

  1. Jmeter笔记(Ⅱ)使用Jmeter实现轻量级的接口自动化测试
  2. js--------1.时间
  3. httpservlet获取请求端IP地址
  4. 标题在上边框中的html(fieldset标签)
  5. hdu 2007 - 平方和与立方和
  6. JDK源码 - BitSet的实现
  7. 业界资讯:adobe air 2.7 beta发布
  8. C++11新特性——移动语义,右值引用
  9. 手动创建两个文本文件text1.txt和text2.txt,按要求创建text3.txt
  10. 看完Java的动态代理技术——Pythoner笑了