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

例如,给出 n = 3,生成结果为:

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

class Solution {
public:
vector<string> res;
vector<string> generateParenthesis(int n)
{
if(n == 0)
return res;
DFS("", n, n);
return res;
} //必须满足right >= left
void DFS(string str, int left, int right)
{
if(left == 0 && right == 0)
{
res.push_back(str);
return ;
}
char c = 0;
if(left == right)
{
c = '(';
DFS(str + c, left - 1, right);
}
else
{
for(int i = 0; i < 2; i++)
{
if(i == 0 && left > 0)
{
c = '(';
DFS(str + c, left - 1, right);
}
else if(i == 1 && right > 0)
{
c = ')';
DFS(str + c, left, right - 1);
}
}
}
}
};

Leetcode22.Generate Parentheses括号生成的更多相关文章

  1. 【LeetCode】22. Generate Parentheses 括号生成

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:括号, 括号生成,题解,leetcode, 力扣,Pyt ...

  2. Leetcode22. Generate Parentheses(生成有效的括号组合)

    (尊重劳动成果,转载请注明出处:http://blog.csdn.net/qq_25827845/article/details/74937307冷血之心的博客) 题目如下:

  3. [LeetCode]22. Generate Parentheses括号生成

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

  4. Generate parentheses,生成括号对,递归,深度优先搜索。

    问题描述:给n对括号,生成所有合理的括号对.比如n=2,(()),()() 算法思路:利用深度优先搜索的递归思想,对n进行深度优先搜索.边界条件是n==0:前面电话号组成字符串也是利用dfs. pub ...

  5. LeetCode22 Generate Parentheses

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

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

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

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

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

  8. generate parentheses(生成括号)

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

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

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

随机推荐

  1. opencv-阈值分割

    关于自适应阈值,可参考:Wellner 自适应阈值二值化算法 一.大津法OTSU(最大类间方差法) 参考:非黑即白——图像分割入门篇之Otsu阈值 自适应阈值分割—大津法(OTSU算法)C++实现 灰 ...

  2. 图书-技术-SpringBoot:《Spring Boot2 + Thymeleaf 企业应用实战》

    ylbtech-图书-技术-SpringBoot:<Spring Boot2 + Thymeleaf 企业应用实战> <Spring Boot 2+Thymeleaf企业应用实战&g ...

  3. PAT甲级——A1084 Broken Keyboard

    On a broken keyboard, some of the keys are worn out. So when you type some sentences, the characters ...

  4. 关于socket的setsockopt的使用

    关于setsockopt的使用 学习python的时候学习到了socket,其中有个setsockopt方法的使用,于是乎整理一下关于这个方法的一些内容. 本节目录 一 功能描述 二 用法(getso ...

  5. VC文件操作

    VC文件操作 重命名文件: 注意: 该操作对文件夹一样有效! CFileFind Finder; CString sOldPath = _T("D:\\tt.txt"); CStr ...

  6. [转]C#中用NamedPipe进程间通信

    转自:http://blog.csdn.net/jinjazz/archive/2009/02/03/3861143.aspx 本文只是一个测试例子,核心代码是kernel32.dll中的一组wind ...

  7. 修改linux命令行的提示符PS1

    # If not running interactively, don't do anything [ -z "$PS1" ] && return # check ...

  8. HZOI20190803 A,C题

    题目链接:https://www.cnblogs.com/Juve/articles/11295333.html A: 考场上只有70分... 发现几个性质:特殊性质1:在两条链上,看它是fib第几项 ...

  9. js中call、apply、bind的区别和应用

    一.总体概述 这三个方法都是改变函数本身的this指向的,但是略有不同 1.call 使用方法:函数名.call(this指向,展开的参数列表); 如果传入的this指向是null或者是undifin ...

  10. psu online course

    https://onlinecourses.science.psu.edu/statprogram/programs Graduate Online Course Overviews Printer- ...