【题解】【排列组合】【回溯】【Leetcode】Generate Parentheses
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:
"((()))", "(()())", "(())()", "()(())", "()()()"
思路:
有关Parentheses的题目也是比较常见的,除了Generate Parentheses还有Valid Parentheses,Longest Valid Parentheses
回溯用递归写是性价比最高的,基本思想就是从前往后填字符保证'('的个数不能比')'少,且'('的个数不能比n多
代码:
void generateAtDepth(vector<string> &Parentheses, string &p, int lefts, int rights, int depth, int n){
if(depth == *n-){
p[depth] = ')';
Parentheses.push_back(p);
return;//返回值为void的函数不能忘了return啊!
}
if(lefts < n){//注意不能出现((()
p[depth] = '(';
generateAtDepth(Parentheses, p, lefts+, rights, depth+, n);
}
if(lefts > rights){
p[depth] = ')';
generateAtDepth(Parentheses, p, lefts, rights+, depth+, n);
}
}
vector<string> generateParenthesis(int n) {
vector<string> Parentheses;
if(n < ) return Parentheses;
string p(*n, '*');//一定要指定初始化字符
generateAtDepth(Parentheses, p, , , , n);
return Parentheses;
}
【题解】【排列组合】【回溯】【Leetcode】Generate Parentheses的更多相关文章
- N-Queens And N-Queens II [LeetCode] + Generate Parentheses[LeetCode] + 回溯法
回溯法 百度百科:回溯法(探索与回溯法)是一种选优搜索法,按选优条件向前搜索,以达到目标.但当探索到某一步时,发现原先选择并不优或达不到目标,就退回一步又一次选择,这样的走不通就退回再走的技术为回溯法 ...
- [LeetCode]Generate Parentheses题解
Generate Parentheses: Given n pairs of parentheses, write a function to generate all combinations of ...
- LeetCode: Generate Parentheses 解题报告
Generate ParenthesesGiven n pairs of parentheses, write a function to generate all combinations of w ...
- [LeetCode] Generate Parentheses 生成括号
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- LeetCode Generate Parentheses (DFS)
题意 Given n pairs of parentheses, write a function to generate all combinations of well-formed parent ...
- LeetCode: Generate Parentheses [021]
[称号] Given n pairs of parentheses, write a function to generate all combinations of well-formed pare ...
- LeetCode——Generate Parentheses
Description: Given n pairs of parentheses, write a function to generate all combinations of well-for ...
- [bzoj2729][HNOI2012]排队 题解 (排列组合 高精)
Description 某中学有 n 名男同学,m 名女同学和两名老师要排队参加体检.他们排成一条直线,并且任意两名女同学不能相邻,两名老师也不能相邻,那么一共有多少种排法呢?(注意:任意两个人都是不 ...
- LeetCode Generate Parentheses 构造括号串(DFS简单题)
题意: 产生n对合法括号的所有组合,用vector<string>返回. 思路: 递归和迭代都可以产生.复杂度都可以为O(2n*合法的括号组合数),即每次产生出的括号序列都保证是合法的. ...
- 回溯法 Generate Parentheses,第二次总结
class Solution { public: vector<string> ans; void helper(string& cur, int left, int right, ...
随机推荐
- js中Array自定义contains, indexOf, delete方法.
Array.prototype.contains = function (elem) { for (var i = 0; i < this.length; i++) { if (this[i] ...
- 关于Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)的问题
找不到mysql socket的问题,我最近碰到了好多次重装系统以前,我的mysql,apache,php都是自己编译安装的,当时并没有碰到这些问题,重装系统以后,我的mysql是通过yum安装的,a ...
- 表单_post提交方式和get的区别,元素集
提交方式及表单域的name属性 使用form表单一种是post提交方式,一种是get提交方式,它们以method属性来定义,如果没有指定method属性,默认get方式提交. 表单域必须配合name属 ...
- 计算excel列的名字
#include <iostream> using namespace std; int main() { unsigned int column; cin>> ...
- GCJ 2015-Qualification-C Dijkstra 特殊注意,展开 难度:2
https://code.google.com/codejam/contest/6224486/dashboard#s=p2 题目中的新运算满足传递性不满足自反性,满足传递性则可以先计算后面的部分再计 ...
- NOIP2004 解题报告
第一题:津津的零花钱一直都是自己管理.每个月的月初妈妈给津津300元钱,津津会预算这个月的花销,并且总能做到实际花销和预算的相同. 为了让津津学习如何储蓄,妈妈提出,津津可以随时把整百的钱存在她那里, ...
- mac下安装apache+php+mysql
运行“sudo apachectl start”,再输入帐号密码,这样Apache就运行了. 运行“sudo apachectl -v”,你会看到Mac OS X 10.6.3中的Apache版本号: ...
- linux下xampp集成包安装配置方法
1.查看你linux系统的位数,是32位的还是64位的.使用uname -a命令查看. 显示有 x86_64则说明你是64位内核, 跑的是64位的系统. i386, i686说明你是32位的内核, 跑 ...
- routeProvider
In a previous post about testing I mentioned that route resolves can make authoring unit tests for a ...
- iOS app调试的黑魔法--第三方库
http://www.cocoachina.com/ios/20140928/9785.html