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:

"((()))", "(()())", "(())()", "()(())", "()()()"

思路:

有关Parentheses的题目也是比较常见的,除了Generate Parentheses还有Valid ParenthesesLongest Valid Parentheses

回溯用递归写是性价比最高的,基本思想就是从前往后填字符保证'('的个数不能比')'少,且'('的个数不能比n多

代码:

 void generateAtDepth(vector<string> &Parentheses, string &p, int lefts, int rights, int depth, int n){
if(depth == *n-){
p[depth] = ')';
Parentheses.push_back(p);
return;//返回值为void的函数不能忘了return啊!
}
if(lefts < n){//注意不能出现((()
p[depth] = '(';
generateAtDepth(Parentheses, p, lefts+, rights, depth+, n);
}
if(lefts > rights){
p[depth] = ')';
generateAtDepth(Parentheses, p, lefts, rights+, depth+, n);
}
}
vector<string> generateParenthesis(int n) {
vector<string> Parentheses;
if(n < ) return Parentheses;
string p(*n, '*');//一定要指定初始化字符
generateAtDepth(Parentheses, p, , , , n);
return Parentheses;
}

【题解】【排列组合】【回溯】【Leetcode】Generate Parentheses的更多相关文章

  1. N-Queens And N-Queens II [LeetCode] + Generate Parentheses[LeetCode] + 回溯法

    回溯法 百度百科:回溯法(探索与回溯法)是一种选优搜索法,按选优条件向前搜索,以达到目标.但当探索到某一步时,发现原先选择并不优或达不到目标,就退回一步又一次选择,这样的走不通就退回再走的技术为回溯法 ...

  2. [LeetCode]Generate Parentheses题解

    Generate Parentheses: Given n pairs of parentheses, write a function to generate all combinations of ...

  3. LeetCode: Generate Parentheses 解题报告

    Generate ParenthesesGiven n pairs of parentheses, write a function to generate all combinations of w ...

  4. [LeetCode] Generate Parentheses 生成括号

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

  5. LeetCode Generate Parentheses (DFS)

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

  6. LeetCode: Generate Parentheses [021]

    [称号] Given n pairs of parentheses, write a function to generate all combinations of well-formed pare ...

  7. LeetCode——Generate Parentheses

    Description: Given n pairs of parentheses, write a function to generate all combinations of well-for ...

  8. [bzoj2729][HNOI2012]排队 题解 (排列组合 高精)

    Description 某中学有 n 名男同学,m 名女同学和两名老师要排队参加体检.他们排成一条直线,并且任意两名女同学不能相邻,两名老师也不能相邻,那么一共有多少种排法呢?(注意:任意两个人都是不 ...

  9. LeetCode Generate Parentheses 构造括号串(DFS简单题)

    题意: 产生n对合法括号的所有组合,用vector<string>返回. 思路: 递归和迭代都可以产生.复杂度都可以为O(2n*合法的括号组合数),即每次产生出的括号序列都保证是合法的. ...

  10. 回溯法 Generate Parentheses,第二次总结

    class Solution { public: vector<string> ans; void helper(string& cur, int left, int right, ...

随机推荐

  1. Eclipse 反编译器

    Help-->Install New SoftWare 贴上反编译地址:http://opensource.cpupk.com/decompiler/update/ 选择add,一路向北,起飞.

  2. 使用ServerSocket创建TCP服务器端

    在两个通信实体没有建立虚拟链路之前,必须有一个通信实体先做出“主动姿态”,主动接受来自其他通信实体的连接请求. Java中能接受其它通信实体连接请求的类是ServerSocket,ServerSock ...

  3. sql类型转换

    CAST 和 CONVERT 将某种数据类型的表达式显式转换为另一种数据类型.CAST 和 CONVERT 提供相似的功能. 语法 使用 CAST: CAST ( expression AS data ...

  4. HDU 1166 单点更新,区间求和

    敌兵布阵 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

  5. 利用smarty call函数实现无限极分类

    定义一个function {function name=menu level=0} <ul class="level{$level}"> {foreach $data ...

  6. 内工大acm校赛--整理代码

    题目:小明搜到一行无缩进无换行代码,请帮小明整理代码.无for语句和case语句,而且只有一个主函数.你只要控制注意“:”“{”“}”这三个符号带来的缩进和换行效果就行. Input: 输入只有一行, ...

  7. tornado介绍

    一.定义 tornado是一个异步非阻塞模型的服务器(tcp/http).web框架. 二.特性 1.高并发 原因:其一,网络事件循环部分根据操作系统选择最高效的,如Linux会是epoll: 其二, ...

  8. touch ImageView

    package com.example.touchdemo; import android.os.Bundle;import android.app.Activity;import android.u ...

  9. VMware-workstation-full-10.0.3-1895310 CN

    Name: VMware-workstation-full-10.0.3-1895310.exe发行日期: 2014-07-01内部版本号: 1895310文件大小: 491 MB文件类型: exe ...

  10. Java基础毕向东day02

    1. 常量 null 等特殊 2.标识符 数字-字母-下划线,数字不能开头 3.二进制 1> 二进制计算方法. 2>常用二进制. 1        1      0       0   1 ...