题目

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. c#特性学习(1)

    C#中的特性我认为可以理解为Java中的注解,见名知意,就是描述这个类或是属性的一个信息,是一个语法糖.原理运用的是反射,下面我来演示一下它的原理.其中引用了软谋的代码. 举一个栗子.我们在做code ...

  2. python中文件操作

      打印进度条

  3. SSH 项目整合

    SSH整合:spring + springmvc + hibernate 1.1 生成Maven项目:ar_ssh 1.2 添加jar包:pom.xml 与ssm相比,主要添加了spring与hibe ...

  4. Json4:使用json-lib解析、生成Json

    特征:1.包多2.JSONObject.fromObject import net.sf.json.JSONObject; public class JsonLib { public static v ...

  5. SQL Server 导入超大脚本

    另外使用window server 版操作系统,执行脚本文件比普通版操作系统大大提升大小限制. 在执行SQL脚本的时候要是出现了这些情况我咋办呢? 步入正轨 应用场景:服务器升级,比如原来是2003的 ...

  6. shell脚本遍历子目录

    #!/bin/bashsource /etc/profile tool_path=/data/rsync_clientroot_path=/data/log ####yyyy-mm-dd¸ñʽdat ...

  7. JQ获取选中select 标签的值

    Jq://#ses为select 标签的Id$("#ses option:selected").val(); $("#ses option:selected") ...

  8. java重写LinkedList

    LinkedList重写类LinkList.java: import java.util.LinkedList;import java.util.List; public class LinkList ...

  9. Python Twisted系列教程3:初步认识Twisted

    作者:dave@http://krondo.com/our-eye-beams-begin-to-twist/ 译者:杨晓伟(采用意译) 可以从这里从头开始阅读这个系列. 用twisted的方式实现前 ...

  10. 《C++数据结构-快速拾遗》 树结构

    1.简单的二叉树结构 #include <iostream> using namespace std; typedef int DATA; //建立二叉树的简单结构 typedef str ...