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学分析 - 单因素方差分析

    单因素方差分析(One-Way Analysis of Variance) 判断控制变量是否对观测变量产生了显著影响 分析步骤 1. 建立检验假设 - H0:不同因子水平间的均值无差异 - H1:不同 ...

  2. 精通Spring Boot---使用@ControllerAdvice处理异常

    在Spring 3.2中,新增了@ControllerAdvice.@RestControllerAdvice 注解,可以用于定义@ExceptionHandler.@InitBinder.@Mode ...

  3. Oracle两种临时表的创建与使用详解

    ORACLE数据库除了可以保存永久表外,还可以建立临时表temporary tables.这些临时表用来保存一个会话SESSION的数据,或者保存在一个事务中需要的数据.当会话退出或者用户提交comm ...

  4. stm32基本定时器timer6的原理与使用

    /********************基本定时器 TIM 参数定义,只限 TIM6.7************/ /* 一.定时器分类 STM32F1 系列中,除了互联型的产品,共有 8 个定时器 ...

  5. (PowerDesigner&Sqlite)PD中设计完表后,将其导入数据库中

    本人连接过SQLServer跟SQLite Ⅰ.SQLServer,百度,转一下:http://jingyan.baidu.com/article/7f766daf465e9c4101e1d0d5.h ...

  6. React-表单操作

    用户在表单填入的内容,属于用户跟组件的互动,所以不能用 this.props 读取 <!DOCTYPE html> <html lang="zh-cn"> ...

  7. Careercup - Microsoft面试题 - 5672369481842688

    2014-05-12 06:27 题目链接 原题: Find the max height of a binary tree. 题目:计算二叉树的最大高度. 解法:最大高度?高度不就是最深的叶子节点到 ...

  8. Wordpress 数据库查询错误 Call to a member function get_results() on null

    在插件中的一个文件使用如下代码,无法查询 <body> <?php global $wpdb; $sql = ""; $sql = "SELECT * ...

  9. Visual C++网络五子棋游戏源代码

    说明:网络对战版的五子棋,VC++游戏源码,带音乐,可设置网络最终网络下棋,通过源代码你将了解到设置菜单状态.服务器端口申请.客户机申请连接.发送数据.游戏编写.监听和使用套接字.主菜单对象定义等基础 ...

  10. 自己搭建一个记笔记的环境记录(leanote)

    一直在找一个开源的记笔记的软件,偶然看到leanote.竟然还是开源的,还是国人开发的果断mark了.自己在电脑上搭建了一个挺好玩的.可以记录一些不给别人看的小秘密. 下面是步骤记录,当然可以到官网上 ...