【题解】【排列组合】【回溯】【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, ...
随机推荐
- AES加密 16进制与二进制转换
import java.security.Key; import javax.crypto.Cipher; import javax.crypto.KeyGenerator; import javax ...
- C#学习7.31判断体重是否超标
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- 使用MediaPlayer播放音频-----之二
MediaPlayer播放不同来源的音频文件: 一.播放应用的资源文件 1.调用MediaPlayer的create(Context context , int resid)方法加载指定资源文件. ...
- Phonegap hello world 不容易啊~!
今天一个项目要用phonegap,当初就是觉得phonegap配置太tmd的麻烦了,所以转头appcan,但今天项目必须用-- 先是看到官方说用nodejs装,tmd的,总是重复同一个错误,安装不起, ...
- Topcoder SRM 584 DIV1 600
思路太繁琐了 ,实在不想解释了 代码: #include<iostream> #include<cstdio> #include<string> #include& ...
- [转]jQuery Pagination Ajax分页插件中文详解
在做项目时需要用到在前端页面中需要实现分页显示的功能,类似于博客园下面的分页导航.从网上找了几个,觉得下面这个使用起来非常简单,也很方便.特在这里记录一下. 以下为文章原文. 中文项目地址:http: ...
- nginx初识
- java抽象类与接口 详解
在面向对象的概念中,我们知道所有的对象都是通过类来描绘的,但是并不是所有的类都是用来描绘对象的,如果一个类中没有包含足够的信息来描绘一个具体的对象,这样的类就是抽象类. 抽象类往往用来表征我们在对问题 ...
- 在ubunut下使用pycharm和eclipse进行python远程调试
我比较喜欢Pycharm,因为这个是JetBrains公司出的python IDE工具,该公司下的java IDE工具--IDEA,无论从界面还是操作上都甩eclipse几条街,但项目组里有些人使用e ...
- 创建和运行shell脚本程序
转载请标明http://www.cnblogs.com/winifred-tang94/ 要创建一个shell脚本程序,首先新建一个文本文件,然后在这个文本文件中按照shell编程规则输入shell命 ...