LeetCode OJ-- Generate Parentheses *
https://oj.leetcode.com/problems/generate-parentheses/
输入n产生n个( ,n个 )组成的所有合法的括号组合。
现在纸上画画,找到规律:
1.每一个位置上 ( 的个数必须 >= ) 的个数
2.如果 ( 的个数是n了,则只能再画 ) 了
3.否则,既可以是 ( 又可以是 )
4.初始第一个位置是 (
5.当 string的长度是 2*n 的时候停止
使用递归调用:
class Solution {
public:
vector<string> generateParenthesis(int n) {
vector<string> ans;
if(n<=)
return ans;
string ansPiece;
ansPiece.append("(");
subGenerateParenthesis(n,ans,ansPiece);
return ans;
}
void subGenerateParenthesis(int &n, vector<string> &ans, string ansPiece)
{
if(ansPiece.size() == *n)
{
ans.push_back(ansPiece);
return;
}
int leftNum = ;
int rightNum = ;
for(int i = ;i<ansPiece.size();i++)
{
if(ansPiece[i]=='(')
leftNum++;
else
rightNum++;
}
if(leftNum==n )
{
ansPiece.push_back(')');
subGenerateParenthesis(n,ans,ansPiece);
}
else if(leftNum == rightNum)
{
ansPiece.push_back('(');
subGenerateParenthesis(n,ans,ansPiece);
}
else
{
string ansPiece2 = ansPiece;
ansPiece.push_back('(');
subGenerateParenthesis(n,ans,ansPiece);
ansPiece2.push_back(')');
subGenerateParenthesis(n,ans,ansPiece2);
}
}
};
LeetCode OJ-- Generate Parentheses *的更多相关文章
- 【题解】【排列组合】【回溯】【Leetcode】Generate Parentheses
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- LeetCode 022 Generate Parentheses
题目描述:Generate Parentheses Given n pairs of parentheses, write a function to generate all combination ...
- leetcode@ [22]Generate Parentheses (递归 + 卡特兰数)
https://leetcode.com/problems/generate-parentheses/ Given n pairs of parentheses, write a function t ...
- leetcode之 Generate Parentheses
题目:http://oj.leetcode.com/problems/generate-parentheses/ 描述:给定一个非负整数n,生成n对括号的所有合法排列. 解答: 该问题解的个数就是卡特 ...
- [LeetCode] 22. Generate Parentheses 生成括号
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- LeetCode 22. Generate Parentheses
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- 【leetcode】Generate Parentheses
题目简述: Given n pairs of parentheses, write a function to generate all combinations of well-formed par ...
- 【leetcode】 Generate Parentheses (middle)☆
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- 【JAVA、C++】LeetCode 022 Generate Parentheses
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- Java [leetcode 22]Generate Parentheses
题目描述: Given n pairs of parentheses, write a function to generate all combinations of well-formed par ...
随机推荐
- python3 提成计算
题目 企业发放的奖金根据利润提成. 利润(I)低于或等于10万元时,奖金可提10%: 利润高于10万元,低于20万元时,低于10万元的部分按10%提成,高于10万元的部分,可提成7.5%: 20万到4 ...
- 动态规划:HDU2159-FATE(二维费用的背包问题)
FATE Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submi ...
- James Bach Rapid Test的感受
前阶段拜读过James大神的快速测试,英文水平有限,阅读起来有点吃力,虽然想亲自参加大神的培训,一直没有机会,不过阿里牛人参加大神的培训,并总结的不错,现在谈谈自己的感想和看法. 进入测试行业不少年了 ...
- .net core 项目加载提示项目文件不完整缺少预期导入的解决办法
今天把在远端的仓库的代码在另一台电脑上拷贝下来,电脑上.net core 环境也已经安装了,但是发现有几个项目没有加载成功,然后重新加载项目,vs2017却提示 项目文件不完整,缺少预期导入 查看错误 ...
- 平衡树 - Luogu 1486 郁闷的出纳员
这么久没写平衡树了,再来一发... P1486 郁闷的出纳员 题目描述 OIER公司是一家大型专业化软件公司,有着数以万计的员工.作为一名出纳员,我的任务之一便是统计每位员工的工资.这本来是一份不错的 ...
- centos使用--vsftpd配置
目录 1 在服务器配置FTP服务 1.1 在root权限下,通过如下命令安装Vsftp(以CentOS系统为例): 1.2 在启动vsftpd服务之前,需要登录云服务器修改配置文件,禁用匿名登录. 1 ...
- Python操作MySQL+Redis+MongoDB
1-1 python操作三大主流数据库导学篇 1-2 数据库简介 1-3 MySQL简介 2-1 MySQL安装及配置 2-2 MySQL图形化管理工具 2-3 SQL语法基础-创建并使用数据库 2- ...
- Java 语言概述与开发环境(1)
目录: 一.计算机语言的发展史 二.Java语言的简述 三.Java的特点 四.java语言的运行环境及环境变量的配置 五.Dos的常见命令 六.第一个java程序-HelloWord ...
- Android数据储存之SQLiteDatabase SQLiteOpenHelper类的简单使用
SQLiteOpenHelper 简介: SQLiteOpenHelper是一个借口!所以不能直接实例化!那我们想要得到SQLiteOpenHelper对象就需要实现该接口!创建该接口的实现类对象! ...
- LAMP第二部分apache的配置
1. 下载discuz! mkdir /data/wwwcd /data/wwwmv /root/Discuz_X3.2_SC_GBK.zip .wget http://download.comsen ...