题目描述:Generate Parentheses

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

For example, given n = 3, a solution set is:

"((()))", "(()())", "(())()", "()(())", "()()()"

分析:

递归

代码如下:

class Solution {
private:
vector<string> ret;
public:
void solve(int dep, int maxDep, int leftNum, int leftNumTotal, string s)
{
//如果(个数超出,return
if (leftNumTotal * 2 > maxDep)
return; //如果长度足够,return
if (dep == maxDep){
ret.push_back(s);
return;
} for(int i = 0; i < 2; i++) //加 (
if (i == 0)
solve(dep + 1, maxDep, leftNum + 1, leftNumTotal + 1, s + '(');
//加 )
else if (leftNum > 0)
solve(dep + 1, maxDep, leftNum - 1, leftNumTotal, s + ')');
} vector<string> generateParenthesis(int n){
ret.clear();
solve(0, 2 * n, 0, 0, "");
return ret;
}
};

Java:

    public List<String> generateParenthesis(int n) {

        ArrayList<String> result = new ArrayList<String>();
dfs(result, "", n, n);
return result; } public void dfs(ArrayList<String> result, String s, int left, int right) { if (left > right) {
return;
} if (left == 0 && right == 0) {
result.add(s);
return;
} if (left > 0) {
dfs(result, s + "(", left - 1, right);
} if (right > 0) {
dfs(result, s + ")", left, right - 1);
} }

LeetCode 022 Generate Parentheses的更多相关文章

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

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

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

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

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

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

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

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

  5. LeetCode 22. Generate Parentheses

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

  6. 【leetcode】Generate Parentheses

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

  7. 【leetcode】 Generate Parentheses (middle)☆

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

  8. Java [leetcode 22]Generate Parentheses

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

  9. leetcode之 Generate Parentheses

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

随机推荐

  1. Learn day4 函数参数\变量\闭包\递归

    1.函数描述 # ### 函数 """ (1)函数的定义: 功能 (包裹一部分代码 实现某一个功能 达成某一个目的) (2)函数特点: 可以反复调用,提高代码的复用性,提 ...

  2. Vue项目入门实例

    前言 本文记录Vue2.x + Element-UI + TypeScript语法入门实例 为什么要用TypeScript? 1.TypeScript是JavaScript的超集,利用es6语法,实现 ...

  3. [Luogu P2827] 蚯蚓 (巧妙的模拟)

    题面: 传送门:https://www.luogu.org/problemnew/show/P2827 Solution 看到这题,我们肯定会有一个大胆想法. 那就是直接用堆模拟这个过程. 对于q,我 ...

  4. SQL SERVER级联查询及数据结构《存储过程-递归树形查询》

    --创建表,插入数据 create table tb(id varchar(3) , pid varchar(3) , name varchar(10))insert into tb values(' ...

  5. uniApp朋友圈(参考)

    介绍 功能:回复,点赞(笔芯),评论,图片(最多六张). 码云地址:https://gitee.com/sunliusen/friend 例:

  6. Android基础——项目的文件结构(二)

    Android基础--项目的文件结构(二) AndroidManifest.xml文件分析 [注]此项目文件结构仅限于Android Studio下的Android项目!!! 在一个Android项目 ...

  7. Flink SQL Client初探

    欢迎访问我的GitHub https://github.com/zq2599/blog_demos 内容:所有原创文章分类汇总及配套源码,涉及Java.Docker.Kubernetes.DevOPS ...

  8. 【转】jps 命令使用

    jps(Java Virtual Machine Process Status Tool)是JDK1.5提供的一个显示当前所有java进程pid的命令,简单实用,非常适合在linux/unix平台上简 ...

  9. TCP/IP协议图解

    联网的各个终端之间能否进行交互的软件基础是网络协议栈,目前主流的网络协议栈是TCP/IP协议栈. 1.主机到网络层协议:以太网协议 主机到网络层主要为IP协议和ARP协议提供服务.发送和接收网络数据报 ...

  10. maven 笔记2

    maven 中央工厂的位置:D:\dubbo\apache-maven-3.2.5\lib D:\dubbo\apache-maven-3.2.5\lib pom-4.0.0.xml reposito ...