题目

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. 初始Golang

    Golang初识 字节跳动也就是我们常说的今日头条 1.今日头条基于Go语言构建千亿级微服务的实践 今日头条当前后端服务超过80%的流量是跑在Go构建的服务上 微服务数量超过100个 高峰QPS超过7 ...

  2. man iptables 8

    IPTABLES(8) iptables 1.6.0 IPTABLES(8) NAME iptables/ip6tables — administration tool for IPv4/IPv6 p ...

  3. windows server 2012 本地用户和组

  4. 在Ubuntu16.04下安装 labelImg

    首先按照 http://www.linuxdiyf.com/linux/13934.html 和 http://www.linuxdiyf.com/linux/13934.html  在ubuntu ...

  5. linux 安装scala

    1. 下载scala 到scala官网下载scala https://www.scala-lang.org/download/,目前最新版本是2.12.8 wget https://downloads ...

  6. 打通WordPress和微信公众号

    现在还坚持写博客的人越来越少,我的博客这几年也更新很少.写博客文章的人少了,有不少人都转战到微信公众号里去写文章了.相对于博客,微信公众号(特别是订阅号)是一个相对封闭.去中心化的平台,在移动互联网时 ...

  7. (转)C# WebApi 跨域问题解决方案:CORS

    原文地址:http://www.cnblogs.com/landeanfen/p/5177176.html 阅读目录 一.跨域问题的由来 二.跨域问题解决原理 三.跨域问题解决细节 1.场景描述 2. ...

  8. Java中的Html解析:使用jsoup

    包:jsoup-1.10.2.jar import java.io.File; import java.io.IOException; import org.jsoup.Jsoup; import o ...

  9. CS229 6.2 Neurons Networks Backpropagation Algorithm

    今天得主题是BP算法.大规模的神经网络可以使用batch gradient descent算法求解,也可以使用 stochastic gradient descent 算法,求解的关键问题在于求得每层 ...

  10. Heartbeat+DRBD+MFS高可用

    Heartbeat+DRBD+MFS高可用. 前言:MFS系统简介 组件名称及作用 管理服务器(Managing Server)   简称Master Server 这个组件的角色是管理整个mfs文件 ...