题目描述

给出n对括号,请编写一个函数来生成所有的由n对括号组成的合法组合。

例如,给出n=3,解集为:
"((()))", "(()())", "(())()", "()(())", "()()()"

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:

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


示例1

输入

复制

1

输出

复制

["()"]
示例2

输入

复制

2

输出

复制

["(())","()()"]

class Solution {
public:
    /**
     *
     * @param n int整型
     * @return string字符串vector
     */
    void generateParenthesisAux(int n,int x ,int y,string s,vector<string> &ans){
        if (y==n)  ans.push_back(s);
        if (x<n) generateParenthesisAux(n, x+1, y,  s+"(", ans);
        if (x>y) generateParenthesisAux(n,  x, y+1, s+")", ans);
    }
    vector<string> generateParenthesis(int n) {
        // write code here
        vector <string> ans;
        generateParenthesisAux(n,0,0,"",ans);
        return ans;
    }
};

leetcode128-generate-parentheses的更多相关文章

  1. 72. Generate Parentheses && Valid Parentheses

    Generate Parentheses Given a string containing just the characters '(', ')', '{', '}', '[' and ']', ...

  2. Generate Parentheses

    Generate Parentheses Given n pairs of parentheses, write a function to generate all combinations of ...

  3. [LintCode] Generate Parentheses 生成括号

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

  4. [CareerCup] 9.6 Generate Parentheses 生成括号

    9.6 Implement an algorithm to print all valid (e.g., properly opened and closed) combinations of n-p ...

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

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

  6. leetcode022. Generate Parentheses

    leetcode 022. Generate Parentheses Concise recursive C++ solution class Solution { public: vector< ...

  7. [Leetcode][Python]22: Generate Parentheses

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 22: Generate Parentheseshttps://oj.leet ...

  8. N-Queens And N-Queens II [LeetCode] + Generate Parentheses[LeetCode] + 回溯法

    回溯法 百度百科:回溯法(探索与回溯法)是一种选优搜索法,按选优条件向前搜索,以达到目标.但当探索到某一步时,发现原先选择并不优或达不到目标,就退回一步又一次选择,这样的走不通就退回再走的技术为回溯法 ...

  9. 22. Generate Parentheses(ML)

    22. Generate Parentheses . Generate Parentheses Given n pairs of parentheses, write a function to ge ...

  10. leetcode-algorithms-22 Generate Parentheses

    leetcode-algorithms-22 Generate Parentheses Given n pairs of parentheses, write a function to genera ...

随机推荐

  1. python简单实现论文查重(软工第一次项目作业)

    前言 软件工程 https://edu.cnblogs.com/campus/gdgy/informationsecurity1812 作业要求 https://edu.cnblogs.com/cam ...

  2. 多测师讲解自动化 _rf 变量_高级讲师肖sir

    rf变量 log 打印全局变量 列表变量: 字典变量: 查看当前工程下的变量 紫色表示变量名有误 设置全局变量 设置列表变量 设置字段变量 关键字书写格式问题

  3. 【树形DP】BZOJ 3829 Farmcraft

    题目内容 mhy住在一棵有n个点的树的1号结点上,每个结点上都有一个妹子i. mhy从自己家出发,去给每一个妹子都送一台电脑,每个妹子拿到电脑后就会开始安装zhx牌杀毒软件,第i个妹子安装时间为Ci. ...

  4. docker-搭建单机 kafka+zookeeper

    1 zookeeper   docker run --name zookeeper -p 12181:2181 -d wurstmeister/zookeeper:latest   2 kafka   ...

  5. RabbitMQ消息队列总结

    AMQP[高级消息队列协议] 是一个异步消息传递所使用的应用层协议规范(是线路层协议)AMQP 客户端能够无视消息的来源任意发送和接受信息 队列的使用场景: 1.与业务的主要逻辑无关,但又需要执行,就 ...

  6. __getattr__和__setattr__

    getattr 拦截运算(obj.xx),对没有定义的属性名和实例,会用属性名作为字符串调用这个方法 class F(object): def __init__(self): self.name = ...

  7. 面试官:为什么MySQL的索引要使用B+树,而不是其它树?比如B树?

    InnoDB的一棵B+树可以存放多少行数据? 答案:约2千万 为什么是这么多? 因为这是可以算出来的,要搞清楚这个问题,先从InnoDB索引数据结构.数据组织方式说起. 计算机在存储数据的时候,有最小 ...

  8. ServletResponse使用介绍

    ServletResponse为将响应发送到客户端的对象:Servlet 容器创建 ServletResponse 对象,并将它作为参数传递给Servlet的service 方法,如下图 : Http ...

  9. poj 2229 一道动态规划思维题

    http://poj.org/problem?id=2229 先把题目连接发上.题目的意思就是: 把n拆分为2的幂相加的形式,问有多少种拆分方法. 看了大佬的完全背包代码很久都没懂,就照着网上的写了动 ...

  10. Scala编程 笔记

    date: 2019-08-07 11:15:00 updated: 2019-11-25 20:00:00 Scala编程 笔记 1. makeRDD 和 parallelize 生成 RDD de ...