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. Mac系统给移动硬盘分区(图文)

    刚买的硬盘500G   准备分几个区 移动硬盘分区格式化有3中形式: 1.Mac OS 扩展日志 格式 此格式mac专用,这种格式的硬盘在PC上不可见,可以用来给 Time Machine 备份, T ...

  2. BEC listen and translation exercise 31

    听力练习: All societies have ways of encouraging and enforcing what they view as appropriate behaviour w ...

  3. Hibernate - POJO 类和数据库的映射文件*.hbm.xml

    POJO 类和关系数据库之间的映射可以用一个XML文档来定义. 通过 POJO 类的数据库映射文件,Hibernate可以理解持久化类和数据表之间的对应关系,也可以理解持久化类属性与数据库表列之间的对 ...

  4. Convolutional Neural Networks for Visual Recognition 3

    Gradient Computing 前面我们介绍过分类器模型一般包含两大部分,一部分是score function,将输入的原始数据映射到每一类的score,另外一个重要组成部分是loss func ...

  5. bzoj 1312: Hard Life 01分数规划+网络流

    题目: Description 在一家公司中,人事部经理与业务部经理不和.一次,总经理要求人事部从公司的职员中挑选出一些来帮助业务部经理完成一项任务.人事部经理发现,在公司的所有职员中,有一些人相处得 ...

  6. mouseout与mouseleave的区别

    1 mouseout:当鼠标指针从元素上移开时,发生 mouseout 事件.该事件大多数时候会与 mouseover 事件一起使用. 2 mouseout与 mouseleave 事件不同,不论鼠标 ...

  7. poj 2187 Beauty Contest —— 旋转卡壳

    题目:http://poj.org/problem?id=2187 学习资料:https://blog.csdn.net/wang_heng199/article/details/74477738 h ...

  8. Android TextView跑马灯

    <TextView android:layout_width="fill_parent" android:layout_height="wrap_content&q ...

  9. POJ1001(C++处理大数)

    Exponentiation Time Limit: 500MS   Memory Limit: 10000K Total Submissions: 158025   Accepted: 38470 ...

  10. redis的read error on connection错误解决

    昨日,公司php调用redis报错:read error on connection 2015-01-29 23:59:050.13330000,redis存放的是用户session. 在网上查询,大 ...