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 *的更多相关文章

  1. 【题解】【排列组合】【回溯】【Leetcode】Generate Parentheses

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

  2. LeetCode 022 Generate Parentheses

    题目描述:Generate Parentheses Given n pairs of parentheses, write a function to generate all combination ...

  3. leetcode@ [22]Generate Parentheses (递归 + 卡特兰数)

    https://leetcode.com/problems/generate-parentheses/ Given n pairs of parentheses, write a function t ...

  4. leetcode之 Generate Parentheses

    题目:http://oj.leetcode.com/problems/generate-parentheses/ 描述:给定一个非负整数n,生成n对括号的所有合法排列. 解答: 该问题解的个数就是卡特 ...

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

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

  6. LeetCode 22. Generate Parentheses

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

  7. 【leetcode】Generate Parentheses

    题目简述: Given n pairs of parentheses, write a function to generate all combinations of well-formed par ...

  8. 【leetcode】 Generate Parentheses (middle)☆

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

  9. 【JAVA、C++】LeetCode 022 Generate Parentheses

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

  10. Java [leetcode 22]Generate Parentheses

    题目描述: Given n pairs of parentheses, write a function to generate all combinations of well-formed par ...

随机推荐

  1. Python 文件操作Error: binary mode doesn't take an encoding argument

    Python 报错:ValueError: binary mode doesn't take an encoding argument 在运行文件操作相关功能时报错:ValueError: binar ...

  2. 怎么选取训练神经网络时的Batch size?

    怎么选取训练神经网络时的Batch size? - 知乎 https://www.zhihu.com/question/61607442 深度学习中的batch的大小对学习效果有何影响? - 知乎 h ...

  3. 精通SpringBoot--分页查询功能的实现

    本文将介绍如何实现分页查询功能,推荐使用github的pagehelper插件实现(事实上大家基本都是这么干的),但本文的实现方式和大多数不同,废话少说,现在就带着大家看看区别在哪里.先看pom.xm ...

  4. 20190102(多线程,守护线程,线程互斥锁,信号量,JoinableQueue)

    多线程 多进程: 核心是多道技术,本质上就是切换加保存技术. 当进程IO操作较多,可以提高程序效率. 每个进程都默认有一条主线程. 多线程: 程序的执行线路,相当于一条流水线,其包含了程序的具体执行步 ...

  5. Spark性能优化:shuffle调优

    调优概述 大多数Spark作业的性能主要就是消耗在了shuffle环节,因为该环节包含了大量的磁盘IO.序列化.网络数据传输等操作.因此,如果要让作业的性能更上一层楼,就有必要对shuffle过程进行 ...

  6. 当我们在讨论CQRS时,我们在讨论些神马?

    当我写下这个标题的时候,我就有些后悔了,题目有点大,不太好控制.但我还是打算尝试一下,通过这篇内容来说清楚CQRS模式,以及和这个模式关联的其它东西.希望我能说得清楚,你能看得明白,如果觉得不错,右下 ...

  7. Intellij idea 出现错误 error:java: 无效的源发行版: 8解决方法

    这是由于jdk的版本与项目的要求不一致造成的,如果是maven项目,首先查看一下pom.xml,以我的项目为例: <build> <plugins> <plugin> ...

  8. loj2056 「TJOI / HEOI2016」序列

    当年我还没学cdq的时候在luogu上写过树套树的代码orzzz ref #include <algorithm> #include <iostream> #include & ...

  9. leetcode 【 Remove Duplicates from Sorted Array 】python 实现

    题目: Given a sorted array, remove the duplicates in place such that each element appear only once and ...

  10. IOS开发学习笔记024-UIButton和UIImageView的区别

    一.UIButton和UIImageView的区别 1. UIImageView 默认只能显示一张图片(默认会填充整个ImageView) 设置方法:image/setImage: UIButton ...