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 ...
随机推荐
- [Python]有关pygame库中的flip和update的区别
pygame.display.flip()和pygame.display.update()的用法上的区别: 资料一. 资料二. (资料最后更新时间:2019年1月9日)
- 初学python来进行odoo12版本开发
这是我的第一篇博客.请多多指教! 首先要下载odoo-12的源代码 官方下载路径: https://github.com/odoo/odoo/archive/12.0.zip 随便新 ...
- hadoop 启动or运行mr错误
hadoop 错误:Incorrect configuration: namenode address dfs.namenode.servicerpc-address or dfs.namenode. ...
- 【PyTorch深度学习】学习笔记之PyTorch与深度学习
第1章 PyTorch与深度学习 深度学习的应用 接近人类水平的图像分类 接近人类水平的语音识别 机器翻译 自动驾驶汽车 Siri.Google语音和Alexa在最近几年更加准确 日本农民的黄瓜智能分 ...
- TCP/IP网络编程之套接字类型与协议设置
套接字与协议 如果相隔很远的两人要进行通话,必须先决定对话方式.如果一方使用电话,另一方也必须使用电话,而不是书信.可以说,电话就是两人对话的协议.协议是对话中使用的通信规则,扩展到计算机领域可整理为 ...
- 【word ladder】cpp
题目: Given two words (beginWord and endWord), and a dictionary, find the length of shortest transform ...
- php代码审计 strcmp和MD5函数漏洞
通过get得到三个值,v1,v2,v3. if第一层判断,v1和v2得到的值不一样,但是对它们进行md5加密后的值得相等. if第二层判断,v3得到的值得和$flag的值相等,满足这两个条件输出fla ...
- Javascript 表达式和运算符
属性访问表达式: var o = {x:1, y:{z:3}};//示例对象 var a = [o, 4, [5,6]];//包含对象的数组 console.log(o["x"]) ...
- 软工实践 - 第十次作业 Alpha 冲刺 (2 / 10)
队名:起床一起肝活队 组长博客:https://www.cnblogs.com/dawnduck/p/9960710.html 作业博客:班级博客本次作业的链接 组员情况 组员1(队长):白晨曦 过去 ...
- Kafka 1.0版本发布
Kafka 1.0版本发布 1.0.0 2017年11月1日发布 源码下载: kafka-1.0.0-src.tgz(asc,sha512) 二进制下载: Scala 2.11 - kafka_2.1 ...