LeetCode22 Generate Parentheses
题意:
iven n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.
For example, given n = 3, a solution set is: (Medium)
[
"((()))",
"(()())",
"(())()",
"()(())",
"()()()"
]
分析:
生成满足数量要求的所以可能的匹配的括号。实际就是一道回溯或者深搜题。
当左括号还没有达到数量的时候,可以添加左括号;当右括号比左括号数量少的时候可以添加右括号;
搜索所有情况即可。有了昨天的17题练习过之后,这个回溯写的就比较顺手了。
代码:
class Solution {
void dfs(vector<string>& v, string& s, int n,int left) {
if (s.size() == n) {
v.push_back(s);
return;
}
int right = s.size() - left;
if (left < n / ) {
s.push_back('(');
dfs(v,s,n,left+);
s.pop_back();
}
if (right < left) {
s.push_back(')');
dfs(v,s,n,left);
s.pop_back();
}
return;
}
public:
vector<string> generateParenthesis(int n) {
vector<string> result;
if (n == ) {
return result;
}
string s;
dfs(result,s,*n,);
return result;
}
};
LeetCode22 Generate Parentheses的更多相关文章
- Leetcode22. Generate Parentheses(生成有效的括号组合)
(尊重劳动成果,转载请注明出处:http://blog.csdn.net/qq_25827845/article/details/74937307冷血之心的博客) 题目如下:
- Leetcode22.Generate Parentheses括号生成
给出 n 代表生成括号的对数,请你写出一个函数,使其能够生成所有可能的并且有效的括号组合. 例如,给出 n = 3,生成结果为: [ "((()))", "(()())& ...
- LeetCode 22. 括号生成(Generate Parentheses)
22. 括号生成 22. Generate Parentheses 题目描述 给出 n 代表生成括号的对数,请你写出一个函数,使其能够生成所有可能的并且有效的括号组合. 例如,给出 n = 3,生成结 ...
- 72. Generate Parentheses && Valid Parentheses
Generate Parentheses Given a string containing just the characters '(', ')', '{', '}', '[' and ']', ...
- Generate Parentheses
Generate Parentheses Given n pairs of parentheses, write a function to generate all combinations of ...
- [LintCode] Generate Parentheses 生成括号
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- [CareerCup] 9.6 Generate Parentheses 生成括号
9.6 Implement an algorithm to print all valid (e.g., properly opened and closed) combinations of n-p ...
- 【题解】【排列组合】【回溯】【Leetcode】Generate Parentheses
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- leetcode022. Generate Parentheses
leetcode 022. Generate Parentheses Concise recursive C++ solution class Solution { public: vector< ...
随机推荐
- [POJ] #1005# I Think I Need a Houseboat : 浮点数运算
一. 题目 I Think I Need a Houseboat Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 97512 ...
- javascript里面的闭包,作用域,预解析
函数的作用域 1.全局变量=公用卫生间 2.局部变量=次卧卫生间 局部变量 全局无法使用 局部声明变量不加var的话就变成全局变量(不推荐使用) 3.闭包=次卧的可以用自己的卫生间 ...
- forms
http://www.cnblogs.com/bomo/p/3309766.html http://www.cnblogs.com/leonwang/archive/2013/03/05/294457 ...
- 【原】Hadoop伪分布模式的安装
Hadoop伪分布模式的安装 [环境参数] (1)Host OS:Win7 64bit (2)IDE:Eclipse Version: Luna Service Release 2 (4.4.2) ( ...
- 类似于QQ游戏百万人同时在线的服务器架构实现
http://blog.csdn.net/sodme/article/details/213995 —————————————————————————————————————————————————— ...
- C++11显式虚函数重载
[C++11显式虚函数重载] 在子类中给重载的虚函数加上override, 可以让编译器检察基类是否有这一虚函数.此功能适用于当基类原有的虚函数发生变化,即相当于编译期检察. 而基类,可以给函数加上f ...
- iOS开发中的Get请求和POST请求
//Get请求一般为不涉及到用户的账号密码的网络请求,其中Get请求是等请求内容回来之后,才可以进行下一步的操作 - (void)requestWithGet{ //Get请求: //1.设置请求路径 ...
- OracleCommand.CommandText 无效
OracleCommand insertADataCmd = conn.CreateCommand(); insertBDataCmd.CommandText = SQL OracleParamete ...
- HDU3033I love sneakers!(分组背包)
http://acm.hdu.edu.cn/showproblem.php?pid=3033 本题的意思就是说现在有n种牌子的鞋子,每种品牌有一些不同的鞋,每双鞋子都有一个特定的权值,现在要求每种品牌 ...
- 浅谈iOS IPv6-only 新规
5月份苹果发布新规,对于开发人员只需要做到以下几点就能顺利上线啦! 1.苹果从6月1日起,提供App Store审核的应用必须要兼容面向硬件识别和网络路由的最新互联网协议--IPv6-only标准.也 ...