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:

[
"((()))",
"(()())",
"(())()",
"()(())",
"()()()"
]

AC code:

class Solution {
public:
vector<string> generateParenthesis(int n) {
vector<string> v;
string str = "";
helper(v, str, 0, 0, n);
return v;
}
void helper(vector<string>& v, string str, int left, int right, int n) {
if (right == n) {
v.push_back(str);
return;
}
if (left < n) {
helper(v, str+'(', left+1, right, n);
}
if (right < left) {
helper(v, str+')', left, right+1, n);
}
}
};

Runtime: 0 ms, faster than 100.00% of C++ online submissions for Generate Parentheses.

22. Generate Parentheses (recursion algorithm)的更多相关文章

  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. [LeetCode] 22. Generate Parentheses 生成括号

    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. 22. Generate Parentheses——本质:树,DFS求解可能的path

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

  8. Java [leetcode 22]Generate Parentheses

    题目描述: Given n pairs of parentheses, write a function to generate all combinations of well-formed par ...

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

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

随机推荐

  1. Codeforces 372B Counting Rectangles is Fun:dp套dp

    题目链接:http://codeforces.com/problemset/problem/372/B 题意: 给你一个n*m的01矩阵(1 <= n,m <= 40). 然后有t组询问( ...

  2. jQuery学习(2)

    <script type="text/javascript"> //给网页中所有的<p>元素添加onclick事件 $("p").cli ...

  3. ajax技术返回json如何处理

    json只是一种文本字符串. Smarty是一个使用PHP写出来的模板引擎. ajax如何处理json数据格式 ①json的格式如下: "{属性名:属性值,属性名:属性值}". 因 ...

  4. java:练习超市卖场

    java:练习超市卖场 涉及到:大商品类,具体商品(以书为例),卖场类 Goods,Book,superMart,  商品类Goods: public interface Goods { //商品类 ...

  5. jQuery-中的事件

    [jQuery中的事件] javascript和html之间的交互是通过用户和浏览器操作页面时引发的事件来处理的,虽然传统的javascript能完成这些交互,但事jQuery增加并扩充了基本事件处理 ...

  6. hdu3572线性欧拉筛

    用线性筛来筛,复杂度O(n) #include<bits/stdc++.h> #include<ext/rope> #define fi first #define se se ...

  7. POJ 2309 BST(二叉搜索树)

    思路:除以2^k,找到商为奇数的位置,k为层数,有2^(k+1)-1个节点 这里直接用位运算,x & -x 就求出 2^k 了. #include<iostream> using ...

  8. Android 之 Matrix(转)

    原文:http://www.cnblogs.com/qiengo/archive/2012/06/30/2570874.html#code Android Matrix   Matrix的数学原理 平 ...

  9. 2018.5.31 nRF905 test

    1 试电机:自动控制测试流程(Labview程序,加载扫描仪,自动测试夹具,测试数据保存) 2 USB RF收发器: 含S/N码发送读取功能(S/N:) The specific use please ...

  10. 关于python包,模块,.pyc文件和文件导入理解

    参考文献 一.包 包是一个文件夹,用来存放模块和子包. 包里一般会有一个__init__.py的文件(也可以没有). 包里会有一个__pycache__文件夹,存放.py文件经解释器解释后的中间字节码 ...