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< ...
随机推荐
- 关于 python 的 @property总结和思考
其实关于@property我到处去搜了很多教程来看,因为公司大量使用了oop的编程而我以前很少写,所以现在来重新补过来. 从使用上来说 加了@property之后最明显的区别就是 class Stud ...
- 第二百一十七天 how can I 坚持
JavaScript document.getElementByName()获取数组,for循环,搞了一天,好笨. 明天要下雪了,好冷. 双十一,天猫搞的挺特别啊,晚上抢了个小米红包,不知道买啥,哎 ...
- systemd详解
CentOS 7 使用systemd替换了SysV.Systemd目的是要取代Unix时代以来一直在使用的init系统,兼容SysV和LSB的启动脚本,而且够在进程启动过程中更有效地引导加载服务. s ...
- 简单版问卷调查系统(Asp.Net+SqlServer2008)
1.系统主要涉及以下几个表 问卷项目表(Q_Naire) 问卷题目表(Q_Problem) 题目类型表(Q_ProblmeType) 题目选项表(Q_Options) 调查结果表(Q_Answer) ...
- C++11初始化列表
[C++11之初始化列表] 在C++03中,在严格遵守POD的定义和限制条件的结构及类型上可以使用初始化列表(initializer list),构想是结构或是数组能够依据成员在该结构内定义的顺序通过 ...
- Xcode——创建你自己的Framework
(注:以下内容是基于Xcode7.2.1操作的,版本不一,可能界面内容不同!) 如果你想将你开发的控件与别人分享,一种方法是直接提供源代码文件.然而,这种方法并不是很优雅.它会暴露所有的实现细节,而这 ...
- Spring REST实践之安全
Securing REST Services 一般有六种方式实现的REST服务的安全: Session-based security HTTP Basic Authentication Digest ...
- Oracle:递归查询(树形结构数据)
今天要做一个查询功能:查询某用户所属部门,且包含该部门的所有上级部门信息.偶然找到了一个方法,特意来做个笔记.分享给和我一样的菜鸟,哈哈 查询子节点 1 select * 2 from d_arc_d ...
- Gartner: Hype Cycle for Emerging Technologies-2012 (技术成熟度曲线) [转]
英文稿: The “Hype Cycle for Emerging Technologies” report is the longest-running annual Hype Cycle, p ...
- Regular Expression--Good parts
匹配URL的正则表达式 <!doctype html><html lang="en"><head> <meta charset=" ...