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 ...
随机推荐
- Java多线程-新特征-阻塞队列ArrayBlockingQueue
阻塞队列是Java5线程新特征中的内容,Java定义了阻塞队列的接口java.util.concurrent.BlockingQueue,阻塞队列的概念是,一个指定长度的队列,如果队列满了,添加新元素 ...
- [转]JavaSE 8—新的时间和日期API
为什么我们需要一个新的时间日期API Java开发中一直存在一个问题,JDK提供的时间日期API一直对开发者没有提供良好的支持. 比如,已有的的类(如java.util.Date和SimpleDate ...
- 修改weblogic PermGen
vim /weblogic/Oracle/Middleware/wlserver_10.3/common/bin/commEnv.sh 在第144行,增加环境变量:JAVA_VENDOR=Sun #根 ...
- linux 下echo命令写入文件内容
http://blog.csdn.net/xukai871105/article/details/35834703 echo "Raspberry" > test.txt
- The C10K problem
原文链接:http://www.kegel.com/c10k.html It's time for web servers to handle ten thousand clients simulta ...
- routeProvider
In a previous post about testing I mentioned that route resolves can make authoring unit tests for a ...
- C++-const_cast只能用于指针和引用,对象的const到非const可以用static_cast
Static_cast可以对对象也可以对指针也可以对引用,但是const_cast只可以对指针和引用使用,后者不可以对对象用,如果你要把一个const值转化为非const值只能用隐式执行或通过使用st ...
- 【干货来了】2014年K2房地产IT分享峰会
2014年K2房地产IT分享峰会已圆满落幕,嘉宾们纷纷出招,分享干货,现场妙语连珠不断,高潮迭起. 主题:流程驱动的地产业务管控平台 嘉宾:王寿欣(卓越地产战略与运营管理部 副总经理) 卓越地产应用K ...
- SQL学习心得(转)
http://www.cnblogs.com/lyhabc/p/3732942.html
- scst使用的一些问题
1,编译问题 问题描述: [root@localhost scstadmin]# make cd scstadmin && make all ]: Entering directory ...