题意:

  产生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简单题)的更多相关文章

  1. [LeetCode] Generate Parentheses 生成括号

    Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...

  2. [CareerCup] 9.6 Generate Parentheses 生成括号

    9.6 Implement an algorithm to print all valid (e.g., properly opened and closed) combinations of n-p ...

  3. [LeetCode] Valid Parentheses 验证括号

    Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...

  4. [LintCode] Generate Parentheses 生成括号

    Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...

  5. N-Queens And N-Queens II [LeetCode] + Generate Parentheses[LeetCode] + 回溯法

    回溯法 百度百科:回溯法(探索与回溯法)是一种选优搜索法,按选优条件向前搜索,以达到目标.但当探索到某一步时,发现原先选择并不优或达不到目标,就退回一步又一次选择,这样的走不通就退回再走的技术为回溯法 ...

  6. generate parentheses(生成括号)

    Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...

  7. LeetCode: Generate Parentheses 解题报告

    Generate ParenthesesGiven n pairs of parentheses, write a function to generate all combinations of w ...

  8. [LeetCode]Generate Parentheses题解

    Generate Parentheses: Given n pairs of parentheses, write a function to generate all combinations of ...

  9. [Leetcode] valid parentheses 有效括号对

    Given a string containing just the characters'(',')','{','}','['and']', determine if the input strin ...

随机推荐

  1. C语言指针(一)

    一.指针 定义指针变量 指针指向的数据类型 *指针变量名称; 例: int *p; *作用: 1.在定义变量的时候 * 是一个类型说明符,说明定义的这个变量是一个指针变量 2.在不是定义变量的时候 * ...

  2. EF的Model First

    一,添加ADO.NET实体数据模型(即edmx) 1,添加edmx         新建一个类库项目,项目中添加新项,选择数据/ADO.NET实体数据模型,如下图.   点击添加,实体数据模型向导窗口 ...

  3. 尽量少用Include

    当我们使用EF写查询,尤其是关联到多个表的时候,我们一般习惯使用include关联,但是过多地使用include会带来性能问题.作为替代方法,可以使用多个toList来代替. 改造前:

  4. case when的用法

    国家(country)人口(population)           中国600            美国100            加拿大100            英国200       ...

  5. invalid types 'int[int]' for array subscrip

    定义重复 如 一个int r 与一个 r[i] 重复

  6. POJ 1739

    楼教主男人八题之一... 题目大意: 求从左下角经过所有非障碍点一次到达右下角的方案数 这里不是求回路,但是我们可以考虑,在最下面一行再增加一行,那么就可以当做求此时左下角到右下角的回路总数,那么就转 ...

  7. ssm开发的一点小技巧

    一般使用反转工作生成基础bean如Items然后我们使用的实体类一般是基础bean的拓展类ItemsCustomer,继承自基础类,这个是为了方便对于表字段的更改生成的bean影响减低我们查询一般是使 ...

  8. 记录一些容易忘记的属性 -- UIKeyboard

    //UIKeyboardWillShowNotification这个通知在软键盘弹出时由系统发送    //UIKeyboardWillShowNotification 通知:键盘将要显示的通知    ...

  9. SharePoint开发 - Excel数据导入到SharePoint自定义列表(数据视图方式)

    博客地址 http://blog.csdn.net/foxdave 本篇讲解一个有些新颖的SharePoint实例应用,给甲方做过项目的都有过体会,数据太多了,客户有Excel,要求实现批量导入. 效 ...

  10. C++学习之静态成员

    一.静态数据成员 C++允许将类的数据成员定义为静态成员.静态数据成员是属于类的,整个类只有一个备份,相当于类的全局变量,能够被该类的所有对象共用. 1.静态成员的声明 在类数据成员的声明前加上关键字 ...