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 ...
随机推荐
- .NET 向SQL里写入非Text类型
一般来说,在更新DataTable或是DataSet时,如果不采用SqlParameter,那么当输入的Sql语句出现歧义时,如字符串中含有单引号,程序就会发生错误,并且他人可以轻易地通过拼接Sql语 ...
- php 应用 cpu 100% 调试方法
找出进程占用cpu高的原因. 进程占用cpu高,一般是由于进程长时间占用cpu,又没有主动释放占用.如果想主动释放cpu,可以调用sleep.在写程序的时候,尤其要注意while 等循环的地方. 找出 ...
- C#入门篇6-9:字符串操作 不值一提的函数【不看也行】
// 判断输入的是否全是数字:返回结果:true:全是数字:false:有字幕出现 public static bool Isaccord1(string str) { bool bl = true; ...
- 流控panabit的安装及配置
软件: 在panabit的下载页面上,没有最新的版本.刚开始就是从这个地方下载的,但是有一块网卡怎么也找不到.各种加载网卡驱动,最后失败. 之后,从其论坛中发现了最新的2013.05版本,将ISO刻盘 ...
- zatree第三方插件
Zabbix安装第三方插件zatree2.4.5 1.下载zatree第三方插件https://github.com/spide4k/zatree.git 2.检查PHP环境需要支持php-xml.p ...
- Java并发编程(三)后台线程(Daemon Thread)
后台线程,守护线程(Daemon Thread) 所谓的后台线程,就是指这种线程并不属于程序中不可或缺的部分,因此当所有的非后台线程结束时,程序也就终止了,同时会杀死进程中的所有后台线程.通过setD ...
- Convert.ToInt16 与 Convert.ToInt32 区别
取值的范围不同: int16:-32768 到 32767 int32:-2,147,483,648 到 2,147,483,647
- MyEclipse取消验证Js的两种方法
MyEclipse取消验证Js的两种方法 作者: 字体:[增加 减小] 类型:转载 通过js写一个web工程的相关页面时感觉很卡,修改内存也不行下面有两种解决方法,大家可以尝试下 前言:有时我们通过j ...
- IBM RSA 的语言设置
右键 IBM Rational software Architect for websphere software 快捷方式 ----> 打开文件位置 在 eclipse.ini 文件中添加参数 ...
- iOS多线程之NSThread使用
iOS中的多线程技术 我们在iOS开发项目过程中,为了解决UI界面操作不被耗时操作阻塞,我们会使用到多线程技术.在iOS开发中,我们主要会用到三种多线程操作技术:NSThread,NSOperatio ...