Leetcode22.Generate Parentheses括号生成
给出 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括号生成的更多相关文章
- 【LeetCode】22. Generate Parentheses 括号生成
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:括号, 括号生成,题解,leetcode, 力扣,Pyt ...
- Leetcode22. Generate Parentheses(生成有效的括号组合)
(尊重劳动成果,转载请注明出处:http://blog.csdn.net/qq_25827845/article/details/74937307冷血之心的博客) 题目如下:
- [LeetCode]22. Generate Parentheses括号生成
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- Generate parentheses,生成括号对,递归,深度优先搜索。
问题描述:给n对括号,生成所有合理的括号对.比如n=2,(()),()() 算法思路:利用深度优先搜索的递归思想,对n进行深度优先搜索.边界条件是n==0:前面电话号组成字符串也是利用dfs. pub ...
- LeetCode22 Generate Parentheses
题意: iven n pairs of parentheses, write a function to generate all combinations of well-formed parent ...
- LeetCode 22. 括号生成(Generate Parentheses)
22. 括号生成 22. Generate Parentheses 题目描述 给出 n 代表生成括号的对数,请你写出一个函数,使其能够生成所有可能的并且有效的括号组合. 例如,给出 n = 3,生成结 ...
- [CareerCup] 9.6 Generate Parentheses 生成括号
9.6 Implement an algorithm to print all valid (e.g., properly opened and closed) combinations of n-p ...
- generate parentheses(生成括号)
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- Leetcode之回溯法专题-22. 括号生成(Generate Parentheses)
Leetcode之回溯法专题-22. 括号生成(Generate Parentheses) 给出 n 代表生成括号的对数,请你写出一个函数,使其能够生成所有可能的并且有效的括号组合. 例如,给出 n ...
随机推荐
- PAT甲级——【牛客练习题1002】
题目描述 Given an integer with no more than 9 digits, you are supposed to read it in the traditional Chi ...
- Java之io nio aio 的区别
这个问题最近面试总是遇到,作为一个只会写流水代码的程序员,一脸懵逼.看了网上的解释,看的还是很模糊,说下我对这个的理解. 先引出一个话题,两个大水缸,一个空一个满,让你把一个缸里面的水弄到另一个里面. ...
- C# 检测真实的文件类型函数
private bool IsAllowedExtension(HttpPostedFile hifile) { bool ret = false; System.IO.FileStream fs = ...
- 基于 Kubernetes 实践弹性的 CI/CD 系统
大家好,我是来自阿里云容器服务团队的华相.首先简单解释一下何为 Kubernetes 来帮助大家理解.Kuberentes 是一个生产可用的容器编排系统.Kuberentes 一方面在集群中把所有 N ...
- 2018.10.29安装tensorflow
先安装tensorflow时按照中文社区安装,结果安装的0.5版本与cuda和cudnn版本不一样,后面才知道需要安好对应版本安装. 1.卸载protobuf pip uninstall protob ...
- 19-10-15-Night-E
信心赛??高考赛…… 过程 T1码了暴力+随机化. T2没码完.$Kuku$了 T3写了暴力+ puts("86400\n-1"); 骗了点分. T1 ××你告诉我CF E题是T1 ...
- Spring MVC(一)--SpringMVC的初始化和流程
SpringMVC是Spring提供给WEB应用的MVC框架,MVC框架一般来说由三部分组成: Model:模型层,一般由java bean完成,主要是进行数据库操作: View:视图层,用于前端展示 ...
- mysql基本笔记之二
1.查看当前编码 show variables like '%char%' 2.修改user表中id=1的name 为 A where后面是条件,就是定位 3.符号 > //大于符号 < ...
- 巧用having
mysql学习教程参考:https://www.w3school.com.cn/sql/index.asp where 与 having实例运用参考:https://www.2cto.com/data ...
- Hadoop 伪分布式安装配置