题目描述

给出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. Web前后端:如何分离,如何解耦?

    摘要:在本文中我们一起来探讨一下为什么要在软件开发中进行前后端的分离,如何做到前后端分离,如何解耦. 简单地说,就是要把复杂的问题简单化,把一个从0到N的问题转化为N个0到1的问题.另一个相近的说法就 ...

  2. Tensorflow学习笔记No.4.2

    使用CNN卷积神经网络(2) 使用Tensorflow搭建简单的CNN卷积神经网络对fashion_mnist数据集进行分类 不了解是那么是CNN卷积神经网络的小伙伴可以参考上一篇博客(Tensorf ...

  3. JavaScript写秒表

    1.HTML部分 <div id="div1"> <span id="hour">00</span> <span> ...

  4. 扩展、接管MVC都不会,还说会Spring Boot?

    持续原创输出,点击上方蓝字关注我 目录 前言 Spring Boot 版本 如何扩展MVC? 如何自定义一个拦截器? 什么都不配置为什么依然能运行MVC相关的功能? 如何全面接管MVC?[不推荐] 为 ...

  5. Java防止文件被篡改之文件校验和

    Java防止文件被篡改之文件校验和转载:请注明出处,谢谢! 1.为什么要防止文件被篡改?  答案是显然的,为了保证版权,系统安全性等.之前公司开发一个系统,技术核心是一个科学院院士的研究成果,作为一款 ...

  6. devops-jenkins分布式构建

    1. devops-jenkins分布式构建 1) 点击系统管理 进入节点管理 2) 点击新建节点 3) 进行节点配置在node节点服务器配置,进行java的安装 [root@nginx-backup ...

  7. 同一台电脑同时使用gitHub和gitLab

    工作中我们有时可能会在同一台电脑上使用多个git账号,例如:公司的gitLab账号,个人的gitHub账号.怎样才能在使用gitlab与github时,切换成对应的账号,并且免密?这时我们需要使用ss ...

  8. Redis 的完整安装过程

    Windos 版本安装 Redis 官方并不支持 Window 版本,但是微软公司在 Github 上维护了一个 Windows 版本的 Redis 项目,供 Windows 用户下载使用. 下载地址 ...

  9. 【C/C++编程入门学习】同样是数据类型,链表对比数组?哪一个更香?

    说起链表,第一反应:链表是一种数据类型!它可以用来存储同种类型多个批量数据.   有了这种认知,很容易去联想到数组,它也是一种数据类型,也可以用来存储同种类型的批量数据.初学者往往对数组的印象比较好, ...

  10. 干货分享:用一百行代码做一个C/C++表白小程序,程序员的浪漫!

    前言:很多时候,当别人听到你是程序员的时候.第一印象就是,格子衫.不浪漫.直男.但是程序员一旦浪漫起来,真的没其他人什么事了.什么纪念日,生日,情人节,礼物怎么送? 做一个浪漫的程序给她,放上你们照片 ...