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:

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

求出所有可能的括号组合,在dfs加上回溯条件,右括号数目不能超过左括号,还有就是左括号数最大不能超过n,代码如下:

 class Solution {
public:
vector<string> generateParenthesis(int n) {
dfs(,n*,,,"");
return ret;
} void dfs(int dep, int maxDep, int left, int leftTotal, string curr){
if(leftTotal* > maxDep)
return;
if(dep == maxDep){
ret.push_back(curr);
return;
}
for(int i = ; i < ; i++){
if(i == ){
dfs(dep+, maxDep, left+, leftTotal+, curr+"(");
}else{
if(left)
dfs(dep+, maxDep, left-, leftTotal, curr+")");
}
}
}
private:
vector<string> ret;
};

LeetCode OJ:Generate Parentheses(括号生成)的更多相关文章

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

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

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

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

  3. Leetcode22.Generate Parentheses括号生成

    给出 n 代表生成括号的对数,请你写出一个函数,使其能够生成所有可能的并且有效的括号组合. 例如,给出 n = 3,生成结果为: [ "((()))", "(()())& ...

  4. 【题解】【排列组合】【回溯】【Leetcode】Generate Parentheses

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

  5. LeetCode 022 Generate Parentheses

    题目描述:Generate Parentheses Given n pairs of parentheses, write a function to generate all combination ...

  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. Leetcode22. Generate Parentheses(生成有效的括号组合)

    (尊重劳动成果,转载请注明出处:http://blog.csdn.net/qq_25827845/article/details/74937307冷血之心的博客) 题目如下:

  9. LeetCode 22 Generate Parentheses(找到所有匹配的括号组合)

    题目链接 : https://leetcode.com/problems/generate-parentheses/?tab=Description   给一个整数n,找到所有合法的 () pairs ...

  10. Generate parentheses,生成括号对,递归,深度优先搜索。

    问题描述:给n对括号,生成所有合理的括号对.比如n=2,(()),()() 算法思路:利用深度优先搜索的递归思想,对n进行深度优先搜索.边界条件是n==0:前面电话号组成字符串也是利用dfs. pub ...

随机推荐

  1. SparkStreaming程序设计

    一个简单的 Streamin wordCount object StreamingWordCount { def main(args: Array[String]): Unit = { val spa ...

  2. JVM调优总结(转)

    本文转自:http://my.oschina.net/xishuixixia/blog/132395 常用的调优参数. 1.堆大小 -Xms和-Xmx用于指定堆大小,我们需要将他们俩设置为一样的值,以 ...

  3. centos7开启网卡功能

    centos7安装完成后,网卡默认是关闭的,未分配ip地址 解决办法: 1.cd /etc/sysconfig/network-scripts/ 2.ls查看网卡 3.修改该文件 vi ifcfg-e ...

  4. RabbitMQ学习之(一)_初步了解RabbitMQ、RabbitMQ的使用流程、为什么要使用RabbitMQ、RabbitMQ的应用场景

    初识RabbitMQ RabbitMQ是一个在AMQP协议基础上实现的消息队列系统, 是一个消息代理.它的核心原理非常简单:接收和发送消息.你可以把它想像成一个邮局:你把信件放入邮箱,邮递员就会把信件 ...

  5. 学Git,用Git ①

    本月开始接触到Git版本管理工具,觉得很有意思,在这里总结一下学习Git的一些心得体会. 要在Mac上完整的使用git进行版本管理,需要熟悉Mac终端操作命令和Git操作命令两种命令,索性两种命令加在 ...

  6. bzoj 3450: Tyvj1952 Easy

    Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 411  Solved: 309[Submit][Status][Discuss] Descriptio ...

  7. [转][修]利用matlab绘制地图上的点、线、面

    一.绘制点 %生成背景地图地图   h = worldmap('France'); %读取和显示大陆架   landareas = shaperead('landareas.shp','UseGeoC ...

  8. Understanding and Creating OWIN Middlewares - Part 1

    In my previous article, What is OWIN? A Beginners Guide we learned the basics of OWIN and the benefi ...

  9. git gc内存错误的解决方案

    Auto packing the repository for optimum performance. You may alsorun "git gc" manually. Se ...

  10. 字典树应用 - poj1002

    字典树应用 - poj 1002 Description Businesses like to have memorable telephone numbers. One way to make a ...