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 ...
随机推荐
- 初学js之多组图片切换实例
需求是以上效果展示.话不多说,直接代码显示,不涉及代码优化.已实现功能为目的. 先看html部分: <body> <div class="dream" id=&q ...
- usb gadge驱动设计之我是zero
此处将以zero.c为例进行讲解. 第一次接触zero.c驱动的时候,是因为某项目需要,提供一种usb字符设备,希望能够通过字符设备打开,读取和发送文件.当时能想到的就是zero.c文件,本打算按照z ...
- STM32串口中断实例二
int main(void) { uint8_t a=;//LED高低电压控制 /* System Clocks Configuration */ RCC_Configuration(); //系统时 ...
- Hive UDTF开发指南
在这篇文章中,我们将深入了解用户定义表函数(UDTF),该函数的实现是通过继承org.apache.Hadoop.hive.ql.udf.generic.GenericUDTF这个抽象通用类,UDTF ...
- 递归查询子类sql
--通过父节点查询子节点 WITH TREE AS( SELECT * FROM Role WHERE RoleID = 4 -- 要查询的父 id UNION ALL SELECT Role.* F ...
- 2、HTML基础总结 part-2
1.表单一 <html> <body> <form> 姓名: <input type="text" name="name&quo ...
- 【Decode Ways】cpp
题目: A message containing letters from A-Z is being encoded to numbers using the following mapping: ' ...
- 转:vc与界面开发之间的文章
[很好的一篇文章,很喜欢看同行的心路历程:http://www.vckbase.com/index.php/nv/444.html] 本屌丝在新春放假期间闲来无事,在各大编程论坛溜达了一圈.发现年前的 ...
- MOTCF 没时间解释了 条件竞争漏洞
moctf 没时间解释了 条件竞争漏洞 题目链接 条件竞争: 在本题目中,上传文件的时候服务器无条件的接收任何类型的文件,但是你上传之后服务器会给你的文件内容修改为too slow. 比如你上传了一句 ...
- centos 7 配置ip
1.动态获取ip(前提是你的路由器已经开启了DHCP) 修改网卡配置文件 vi /etc/sysconfig/network-scripts/ifcfg-ens32 (最后一个为网卡名称) 动态 ...