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< ...
随机推荐
- 监听mysql是否挂了
监听mysql是否挂了,如果挂了就重启mysql 方式一: #!/bin/bash pgrep -x mysqld &> /dev/null if [ $? -ne 0 ] then e ...
- POJ 2826 An Easy Problem?!
An Easy Problem?! Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7837 Accepted: 1145 ...
- unigui unidbgrid显示列的合计值
procedure TfrmClient.UniDBGrid1ColumnSummaryResult(Column: TUniDBGridColumn; GroupFieldValue: Varian ...
- HDU 5164Matching on Array(AC自动机)
这是BC上的一道题,当时比赛没有做,回头看看题解,说是AC自动机,想着没有写过AC自动机,于是便试着抄抄白书的模板,硬是搞了我数个小时2000ms时限1800过了= = ! 这里就直接贴上BC的结题报 ...
- .java 文件中只能定义一个public class 且与文件名相同
- CGI 是什么
CGI是公共网关接口,是Java Servlet 的前身,Java Servlet 是运行在服务器端的小程序.
- 【Netbeans】表格的使用
参数两个:一个二维数组赋予数据,一个一位数组赋予属性名
- .NET两种常见上传下载文件方法
1.FTP模式 代码如下: (1)浏览 /// <summary> /// 浏览文件 /// </summary> /// <param name="tbCon ...
- $( document ).ready()&$(window).load()
$( document ).ready() https://learn.jquery.com/using-jquery-core/document-ready/ A page can't be man ...
- jQuery each,避免使用js for循环
What is the difference between $.each(selector) and $(selector).each(): http://stackoverflow.com/que ...