给出 n 代表生成括号的对数,请你写出一个函数,使其能够生成所有可能的并且有效的括号组合。

例如,给出 n = 3,生成结果为:
[
"((()))",
"(()())",
"(())()",
"()(())",
"()()()"
]

思路:

递归:

#include <iostream>
#include <vector>
using namespace std;
void gen(int left,int right,string s,vector<string> &ans)
{
if(right==) {ans.push_back(s); return ;}//右括号用完前,左括号早就用完了。所以返回
else if(left==right) gen(left-,right,s+'(',ans);//剩余的左括号数量=右括号数量时,只能放左括号
else if(left==) gen(left,right-,s+')',ans);//左括号都写进s中去了,后面只能写右括号了
else {gen(left,right-,s+')',ans);//除上面几种情况外,写左写右均可
gen(left-,right,s+'(',ans);
}
} vector<string> generateParenthesis(int n) {
vector<string> ans;
gen(n,n,"",ans);
return ans;
} int main() {
int n=;
vector<string> ans;
ans=generateParenthesis(n);
cout<<ans[]<<endl;
return ;
}

#leetcode刷题之路22-括号生成的更多相关文章

  1. LeetCode刷题笔记-回溯法-括号生成

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

  2. Leetcode之回溯法专题-22. 括号生成(Generate Parentheses)

    Leetcode之回溯法专题-22. 括号生成(Generate Parentheses) 给出 n 代表生成括号的对数,请你写出一个函数,使其能够生成所有可能的并且有效的括号组合. 例如,给出 n ...

  3. #leetcode刷题之路32-最长有效括号

    给定一个只包含 '(' 和 ')' 的字符串,找出最长的包含有效括号的子串的长度. 示例 1:输入: "(()"输出: 2解释: 最长有效括号子串为 "()"示 ...

  4. #leetcode刷题之路20-有效的括号

    #include <iostream> #include <string> #include <stack> using namespace std; bool i ...

  5. python -- leetcode 刷题之路

    第一题 给定一个整数数组和一个目标值,找出数组中和为目标值的两个数. 你可以假设每个输入只对应一种答案,且同样的元素不能被重复利用. 示例: 给定 nums = [2, 7, 11, 15], tar ...

  6. 使用Java+Kotlin双语言的LeetCode刷题之路(三)

    BasedLeetCode LeetCode learning records based on Java,Kotlin,Python...Github 地址 序号对应 LeetCode 中题目序号 ...

  7. 使用Java+Kotlin双语言的LeetCode刷题之路(二)

    BasedLeetCode LeetCode learning records based on Java,Kotlin,Python...Github 地址 序号对应 LeetCode 中题目序号 ...

  8. 使用Java+Kotlin双语言的LeetCode刷题之路(一)

    LeetCode learning records based on Java,Kotlin,Python...Github 地址 序号对应 LeetCode 中题目序号 1 两数之和 给定一个整数数 ...

  9. #leetcode刷题之路40-组合总和 II

    给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合.candidates 中的每个数字在每个组合中只能使用一次.说 ...

随机推荐

  1. Redis的基本操作语句

    注:以下出现的key.value都需要具体 1.String类型的数据存储获取 set key value:设置key的值为value,若存在则覆盖,不存在则自动创建decrby get key:获取 ...

  2. C#多线程顺序依赖执行控制

    在开发过程中,经常需要多个任务并行的执行的场景,同时任务之间又需要先后依赖的关系.针对这样的处理逻辑,通常会采用多线程的程序模型来实现. 比如A.B.C三个线程,A和B需要同时启动,并行处理,且B需要 ...

  3. Linux kill/pkill/killall命令详解

    kill kill(terminate a process)命令用来终止指定的进程, 对于一个后台进程就须用kill命令来终止,我们就需要先使用ps/pidof/pstree/top等工具获取进程PI ...

  4. LNMP-day3-php扩展缓存插件

     perl的编译问题 [root@localhost php5.6.33]# echo 'export LC_ALL=C' >> /etc/profile #设置环境变量,解决后面perl ...

  5. Java日期格式化参数对照表

    Symbol Meaning Presentation Example G era designator Text AD y year Number 2009 M month in year Text ...

  6. codeforces 407D Largest Submatrix 3

    codeforces 407D Largest Submatrix 3 题意 找出最大子矩阵,须满足矩阵内的元素互不相等. 题解 官方做法 http://codeforces.com/blog/ent ...

  7. Spring Cloud(中文版)

    原文链接:Spring Cloud I.云原生应用 Spring Cloud上下文:应用上下文服务 2.1.Bootstrap应用程序上下文 2.2.应用程序上下文层次结构 2.3.更改Bootstr ...

  8. oracle 查看主外键约束

    select a.constraint_name, a.table_name, b.constraint_name from user_constraints a, user_constraints ...

  9. LoadRunner 测试Socket接口函数说明

    lrs_save_param_ex是lrs_save_param的扩展函数,包含了lrs_save_param的基本功能.其函数语法结构如下: int lrs_save_param_ex ( char ...

  10. 【[SDOI2017]序列计数】

    感觉自己的复杂度感人 大概是\(O(p*\pi(m)+p^3logn)\) 还是能过去的 我们看到这么大的数据范围还是应该先想一想暴力怎么写 显然我们可以直接暴力\(dp\) 设\(dp[i][j]\ ...