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:

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

思路:两个递归函数,互相调用

class Solution {
public:
vector<string> generateParenthesis(int n) {
if(n==) return ret; dfsLeft("",,,n);
return ret;
} void dfsLeft(string s, int depthLeft, int depthRight, int n){ //add one left parenthesis
if(depthLeft >= n){ //end left
return;
}
else{
s += '(';
depthLeft++;
dfsRight(s,depthLeft, depthRight, n);
dfsLeft(s,depthLeft, depthRight, n);
}
} void dfsRight(string s, int depthLeft, int depthRight, int n){ //add one right parenthesis
if(depthRight >= n){ //end all
ret.push_back(s);
}
else if(depthRight >= depthLeft){ //end right
return;
}
else{
s += ')';
depthRight++;
dfsLeft(s,depthLeft, depthRight, n);
dfsRight(s,depthLeft, depthRight, n);
}
}
private:
int len;
vector<string> ret; };

更简洁的写法:

class Solution {
public:
vector<string> generateParenthesis(int n) {
if(n == ) return ret; len = n*;
dfsLeft(n, , "");
return ret;
} void dfsRight(int lNum, int rNum, string str){//add right parenthese
while(rNum){
str += ')';
dfsLeft(lNum, --rNum, str);
}
if(str.length() == len){
ret.push_back(str);
}
} void dfsLeft(int lNum, int rNum, string str){//add left parenthese
while(lNum){
str += '(';
dfsRight(--lNum, ++rNum, str);
}
}
private:
int len;
vector<string> ret; };

22.Generate Parentheses (String; Back-Track)的更多相关文章

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

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

  2. 刷题22. Generate Parentheses

    一.题目说明 这个题目是22. Generate Parentheses,简单来说,输入一个数字n,输出n对匹配的小括号. 简单考虑了一下,n=0,输出"";n=1,输出" ...

  3. 22. Generate Parentheses(ML)

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

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

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

  5. 22. Generate Parentheses (recursion algorithm)

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

  6. [LeetCode] 22. Generate Parentheses 生成括号

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

  7. LeetCode 22. Generate Parentheses

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

  8. Java [leetcode 22]Generate Parentheses

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

  9. leetcode@ [22]Generate Parentheses (递归 + 卡特兰数)

    https://leetcode.com/problems/generate-parentheses/ Given n pairs of parentheses, write a function t ...

随机推荐

  1. pshell远程连接服务器

    在页面添加ip    和 端口  还有 用户,我这里填的是服务器root用户 成功之后 端口后是可以改的   首先看下ssh是否启动  rpm -qa | grep ssh 有的话就是vi /etc/ ...

  2. specialized English for automation-Lesson 2 Basic Circuits of Operational Amplifiers

    排版有点乱.... ========================================================================= Operational Ampl ...

  3. swift 分页视图

    var data:NSArray! var scrollView: UIScrollView! var pageCtrl: UIPageControl! override func viewDidLo ...

  4. HDU3530 Subsequence(单调队列)

    题意是说给出一个序列,现在要求出这个序列的一个最长子区间,要求子区间的最大值与最小值的差在[m, k]范围内,求区间长度 做法是维护两个队列,一个维护到当前位置的最大值,一个维护最小值,然后计算当前节 ...

  5. linux mongodb replica set集群安装

    RS集群中mongod的安装和单机一样,只是配置文件略有不同, 单机安装路径linux 下mongodb 3.2.5安装 下面是rs集群的配置文件: systemLog:destination: fi ...

  6. 转 How do GraphQL remote schemas work

    文章转自 prisma 官方博客,写的很不错 In this article, we want to understand how we can use any existing GraphQL AP ...

  7. Layui Table 分页记忆选中

    Layui Table 分页记忆选中 挺好的功能,之前为什么放弃了,哈哈哈! 在最早的版本中,layui 的 table 会记录每页的勾选状态,但很多用户反馈这是 bug,因为当他们获取选中数据时,其 ...

  8. [CLPR] 卷积神经网络的结构

    本文翻译自: http://www.codeproject.com/Articles/16650/Neural-Network-for-Recognition-of-Handwritten-Digi ...

  9. Navicat Premium解决连接mssql报错的问题

    连接名:mssql_172.16.30.21:1433,每次打开查询时就报错. 重启,重转都不好使. 解决办法:去掉“:1433”,因为文件目录不支持“:”,所以一直报错.问题终于得到解决.

  10. mariadb master and salve configure

    mariadb master and salve configure 主从复制配置: master:192.168.8.200 salve:192.168.8.201 主服务器配置: 主服务器需要启动 ...