题目

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:

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

解答

这题用回溯的方法来做。。。

按如下规则往一个括号组成的字符串中添加左括号或右括号:如果左括号没用完,可以直接添加左括号,或者当右括号没用完且当前字符串中左括号的数量大于右括号的数量,那么可以添加右括号。添加左括号不会导致字符串中的括号不匹配,因为添加的左括号肯定能在之后通过添加右括号进行匹配,又因为已经匹配的左右括号可以直接忽略,那么当右括号没用完且当前字符串中左括号的数量大于右括号的数量时,忽略已经匹配的左右括号,剩下的全是左括号,所以此时添加右括号可以匹配,而当右括号没用完且当前字符串中左括号的数量不大于右括号的数量时,此时添加的右括号无法在当前字符串中找到一个左括号进行匹配,也无法和之后添加的左括号进行匹配。

下面是AC的代码:

class Solution {
public:
    vector<string> ans;
    void generator(string str, int left, int right){
        if(left == 0 && right == 0){
            ans.push_back(str);
            return ;
        }
        if(left > 0){
            generator(str + '(', left - 1, right);
        }
        if(right > 0 && left < right){
            generator(str + ')', left, right - 1);
        }
    }
    vector<string> generateParenthesis(int n) {
        string str = "";

        generator(str, n, n);
        return ans;
    }
};

103

LeetCode OJ 22. Generate Parentheses的更多相关文章

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

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

  2. 【一天一道LeetCode】#22. Generate Parentheses

    一天一道LeetCode (一)题目 Given n pairs of parentheses, write a function to generate all combinations of we ...

  3. 【LeetCode】22. Generate Parentheses (2 solutions)

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

  4. 【LeetCode】22. Generate Parentheses (I thought I know Python...)

    I thought I know Python... Actually , I know nothing... 这个题真想让人背下来啊,每一句都很帅!!! Given n pairs of paren ...

  5. 【LeetCode】22. Generate Parentheses 括号生成

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:括号, 括号生成,题解,leetcode, 力扣,Pyt ...

  6. 【leetcode】22. Generate Parentheses

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

  7. LeetCode OJ:Generate Parentheses(括号生成)

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

  8. LeetCode:22. Generate Parentheses(Medium)

    1. 原题链接 https://leetcode.com/problems/generate-parentheses/description/ 2. 题目要求 给出一个正整数n,请求出由n对合法的圆括 ...

  9. 22. Generate Parentheses(ML)

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

随机推荐

  1. Redis 主从+哨兵+监控 (centos7.2 + redis 3.2.9 )

    环境准备: 192.168.0.2  redis01 主 192.168.0.3  redis02 从 192.168.0.4  redis03 从 Redis 主从搭建 一:下载并安装redis软件 ...

  2. 对偶上升法到增广拉格朗日乘子法到ADMM

    对偶上升法 增广拉格朗日乘子法 ADMM 交替方向乘子法(Alternating Direction Method of Multipliers,ADMM)是一种解决可分解凸优化问题的简单方法,尤其在 ...

  3. (转).Net高级进阶,在复杂的业务逻辑下,如何以最简练的代码,最直观的编写事务代码?

    原文地址:http://www.cnblogs.com/1996V/p/7798111.html 示例一和示例二,主要是来讲解 TransactionScope 是什么,为什么要用Transactio ...

  4. The compiler compliance specified is 1.5 but a JRE 1.8 is used

    错误信息: The compiler compliance specified is 1.5 but a JRE 1.8 is used 解决办法: 右击项目>选择Properties 选择Ja ...

  5. mogoDB工具选择及连接<一>

    最近在某微服务项目中需要用到mogoDB 原因是:开源免费.适合互联网公司.大数据量情况下性能比mysql好 咨询过为啥不用oracle,原因你懂得 --费用 好,言归正传: 1.选择工具,使用工具是 ...

  6. 运行vbs脚本

    VBS是基于Visual Basic的脚本语言. VBS的全称是:Microsoft Visual Basic Script Edition.(微软公司可视化BASIC脚本版). 其语言类似Visua ...

  7. Python笔记:字典的fromkeys方法创建的初始value同内存地址问题

    dict中的fromkeys()函数可以通过一个list来创建一个用同一初始value的dict. d = dict.fromkeys(["苹果", "菠萝"] ...

  8. TessorFlow学习 之 手写数字识别的搭建

    手写数字识别的搭建

  9. 【转】Exchange Server 的防火墙开放端口

    关于exchange所用到的端口参阅下面的文档, 适用于exchange2010sp2. http://technet.microsoft.com/en-us/library/bb331973.asp ...

  10. 《算法》第四章部分程序 part 4

    ▶ 书中第四章部分程序,加上自己补充的代码,图的深度优先遍历 ● 无向图的深度优先遍历,有向 / 无向图代码仅若干方法名不同,包括递归和非递归版本,去掉了顶点有效性的检查 package packag ...