LeetCode Generate Parentheses 构造括号串(DFS简单题)
题意:
产生n对合法括号的所有组合,用vector<string>返回。
思路:
递归和迭代都可以产生。复杂度都可以为O(2n*合法的括号组合数),即每次产生出的括号序列都保证是合法的。
方法都是差不多的,就是记录当前产生的串中含有左括号的个数cnt,如果出现右括号,就将cnt--。当长度为2*n的串的cnt为0时,就是答案了,如果当前cnt比剩下未填的位数要小,则可以继续装“(”,否则不能再装。如果当前cnt>0,那么就能继续装“)”与其前面的左括号匹配(无需要管匹配到谁,总之能匹配)。
递归版本
class Solution {
public:
void DFS(vector<string>& ans,string t,int cnt,int n)
{
if(n==) ans.push_back(t);
if(cnt<n) DFS(ans,t+"(",cnt+,n-);
if(cnt>) DFS(ans,t+")",cnt-,n-);
}
vector<string> generateParenthesis(int n)
{
vector<string> ans;
DFS(ans,"",,n*);
return ans;
}
};
AC代码
迭代版本
vector<string> generateParenthesis(int n)
{
vector<string> ans[];
vector<int> cnt[];//空间换时间
int cur=;
ans[].push_back("");
cnt[].push_back();
for(int i=n<<; i>; i--)
{
cur^=;
ans[cur].clear();
cnt[cur].clear();
for(int j=; j<ans[cur^].size(); j++)
{
string &q=ans[cur^][j];
int &c=cnt[cur^][j];
if(c<i)
{
ans[cur].push_back(q+"(");
cnt[cur].push_back(c+);
}
if(c>)
{
ans[cur].push_back(q+")");
cnt[cur].push_back(c-);
}
}
}
return ans[cur];
}
AC代码
LeetCode Generate Parentheses 构造括号串(DFS简单题)的更多相关文章
- [LeetCode] 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] Valid Parentheses 验证括号
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
- [LintCode] Generate Parentheses 生成括号
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- N-Queens And N-Queens II [LeetCode] + Generate Parentheses[LeetCode] + 回溯法
回溯法 百度百科:回溯法(探索与回溯法)是一种选优搜索法,按选优条件向前搜索,以达到目标.但当探索到某一步时,发现原先选择并不优或达不到目标,就退回一步又一次选择,这样的走不通就退回再走的技术为回溯法 ...
- generate parentheses(生成括号)
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- LeetCode: Generate Parentheses 解题报告
Generate ParenthesesGiven n pairs of parentheses, write a function to generate all combinations of w ...
- [LeetCode]Generate Parentheses题解
Generate Parentheses: Given n pairs of parentheses, write a function to generate all combinations of ...
- [Leetcode] valid parentheses 有效括号对
Given a string containing just the characters'(',')','{','}','['and']', determine if the input strin ...
随机推荐
- ASP.NET文章目录导航
ASP.NET文章目录导航 ASP.NET-[读书笔记]-原创:ASP.Net状态管理读书笔记--思维导图 (2013-12-25 10:13) ASP.NET-[潜在危险]-从客户端中检测到有潜在危 ...
- 克隆机器后eth1变为eth0问题
1. 清空该文件 2.进入网络配置文件把HADDR 和UUID注释掉,并重启 3.成功修改eth0 4. 4.可以结合这篇帖子来看 http://www.cnblogs.com/zydev/p/4 ...
- event.srcElement在火狐(FireFox)下的兼容问题。搜索框获得焦点时默认文字变化
前言: 项目中用到了一个功能,搜索框里有默认的文字,当搜索框获得焦点时里面的默认文字消失,如果失去焦点时搜索框内容为空则让里面的内容回复默认!,. 实现: 很轻松的在网上找到了类似代码 $(" ...
- node cheerio
所谓工欲善其事,必先利其器,所以通读了cheerio的API,顺便翻译了一遍,有些地方因为知道的比较少,不知道什么意思,保留了英文,希望各位不吝告诉我,然后一起把这个翻译完成. ###cheerio ...
- IT公司100题-2-设计带min函数的stack
问题描述: 定义栈的数据结构,要求添加一个min 函数,能够得到栈的最小元素. 要求函数min.push 以及pop 的时间复杂度都是O(1). 双倍空间实现: 保存2个栈,分别是元素和当前最小值 ...
- struts中的数据校验
1.struts中如何进行数据校验 在每一个Action类中,数据校验一般都写在业务方法中,比如login().register()等.struts提供了数据校验功能.每个继承自ActionSuppo ...
- bzoj 2154 莫比乌斯反演求lcm的和
题目大意: 表格中每一个位置(i,j)填的值是lcm(i,j) , 求n*m的表格值有多大 论文贾志鹏线性筛中过程讲的很好 最后的逆元我利用的是欧拉定理求解的 我这个最后线性扫了一遍,勉强过了,效率不 ...
- 戴文的Linux内核专题:07内核配置(3)
转自Linux中国 OK,我们还继续配置内核.还有更多功能等待着去配置. 下一个问题(Enable ELF core dumps (ELF_CORE))询问的是内核是否可以生成内核转储文件.这会使内核 ...
- .net异常小总
1. ExecuteReader:CommandText属性尚未初始化 即:没有对sqlCommand对象的CommandText属性赋值,即没有写sql语句. 2. 由于代码已经过优化或者本机框 ...
- Cannot change network to bridged: There are no un-bridged host network adapters解决方法
首先,在你安装上了虚拟机后要确保你也安装了桥接的协议,这可以通过点击右键“网上邻居”,在其中可以看到有两个虚拟出来的网络一个VMnet1,另一个是VMnet8, 如下图所示. 如果没有安装,可以通过下 ...