题目链接

Generate Parentheses - LeetCode

注意点

解法

解法一:递归。当left>right的时候返回(为了防止出现 )( )

class Solution {
public:
void recursion(int left,int right,string str,vector<string> &ret)
{
if(left > right) return;
else if(left== 0&& right == 0) ret.push_back(str);
if(left > 0) recursion(left-1,right,str+"(",ret);
if(right > 0)recursion(left,right-1,str+")",ret);
}
vector<string> generateParenthesis(int n) {
vector<string> ret;
recursion(n,n,"",ret);
return ret;
}
};

解法二:网上看来的方法。也需要递归,把n-1中生成的字符串中的每一个( 后面加上一个() 然后把和这个(配对的)一起去掉。最后加上一个()就形成了所有完整的情况。因为过程中会出现重复的情况,所以用set来保存过程形成的串。

class Solution {
public:
vector<string> generateParenthesis(int n) {
set<string> s;
if(n == 0) s.insert("");
else
{
vector<string> pre = generateParenthesis(n-1);
for(auto p:pre)
{
for(auto i = 0;i < p.size();i++)
{
if(p[i] == '(')
{
p.insert(p.begin()+i+1,'(');
p.insert(p.begin()+i+2,')');
s.insert(p);
p.erase(p.begin() + i + 1, p.begin() + i + 3);
}
}
s.insert("()"+p);
}
}
return vector<string>(s.begin(),s.end());
}
};

解法三:dfs。

class Solution {
public:
void dfs(int n, int left, int right, string str, vector<string> &ret)
{
if(left < n)
{
dfs(n,left+1,right,str+"(",ret);
}
if(right < left)
{
dfs(n,left,right+1,str+")",ret);
}
if(str.size() == n*2)
{
ret.push_back(str);
}
}
vector<string> generateParenthesis(int n) {
vector<string> ret;
dfs(n, 0, 0, "", ret);
return ret;
}
};

小结

  • 一般DFS:
void dfs()
{
if(符合边界条件)
{
///////
return;
} dfs();//某种形式的调用
}
  • 带回溯的DFS:
void dfs(int 当前状态)
{
if(当前状态为边界状态)
{
//记录或输出
return;
}
for(i=0;i<n;i++) //横向遍历解答树所有子节点
{
//扩展出一个子状态。
修改全局变量
if(子状态满足约束条件)
{
dfs(子状态)
}
恢复全局变量//回溯部分
}
}

Generate Parentheses - LeetCode的更多相关文章

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

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

  2. Generate Parentheses——LeetCode

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

  3. Generate Parentheses leetcode java

    题目: Given n pairs of parentheses, write a function to generate all combinations of well-formed paren ...

  4. [Leetcode][Python]22: Generate Parentheses

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 22: Generate Parentheseshttps://oj.leet ...

  5. Leetcode之回溯法专题-22. 括号生成(Generate Parentheses)

    Leetcode之回溯法专题-22. 括号生成(Generate Parentheses) 给出 n 代表生成括号的对数,请你写出一个函数,使其能够生成所有可能的并且有效的括号组合. 例如,给出 n ...

  6. LeetCode 22. 括号生成(Generate Parentheses)

    22. 括号生成 22. Generate Parentheses 题目描述 给出 n 代表生成括号的对数,请你写出一个函数,使其能够生成所有可能的并且有效的括号组合. 例如,给出 n = 3,生成结 ...

  7. 【题解】【排列组合】【回溯】【Leetcode】Generate Parentheses

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

  8. 【LeetCode】22. Generate Parentheses (2 solutions)

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

  9. LeetCode: Generate Parentheses 解题报告

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

随机推荐

  1. Eclipse web项目更改项目名称

    1. 右键工程:Refactor->Rename,更改项目名称: 2. 修改项目目录下:.project文件 <?xml version="1.0" encoding= ...

  2. Received non-all-whitespace CHARACTERS or CDATA event in nextTag(). ,无法整齐打印验证错误。 解析XML文档出现的问题

    在启动keyCloak,想要在standAlone模式下切换数据库,修改standAlone.xml文件时. 在bin/目录下启动standAlone模式出现错误: 10:07:24,799 INFO ...

  3. 使用python实现用微信远程控制电脑

    首先,我们要先看看微信远程控制电脑的原理是什么呢? 我们可以利用Python的标准库控制本机电脑,然后要实现远程的话,我们可以把电子邮件作为远程控制的渠道,我们用Python自动登录邮箱检测邮件,当我 ...

  4. python清空列表的方法

    1.大数据量的list,要进行局部元素删除,尽量避免用del随机删除,非常影响性能,如果删除量很大,不如直接新建list,然后用下面的方法释放清空旧list. 2.对于一般性数据量超大的list,快速 ...

  5. kafka handler

    1.配置kafka 参数文件 在ogg主目录下有示例文件: [root@WH0PRDBRP00AP0013 ogg]# cd AdapterExamples/big-data/kafka/ [root ...

  6. 关于php的array_diff和array_diff_assoc的使用总结

    关于php的array_diff和array_diff_assoc的使用总结 2015-11-07 17:01 184人阅读 评论(0) 收藏 举报  分类: php学习感想(1)  版权声明:本文为 ...

  7. 使用谷歌浏览器调试WEB前端的一些必备调试技巧

    转载:http://www.techug.com/post/chrome-debug-tips.html Chrome的开发者工具是个很强大的东西,相信程序员们都不会陌生,不过有些小功能可能并不为大众 ...

  8. Scrum Meeting 6 -2014.11.12

    今天apec最后一天,大部分任务都差不多了,局部测试问题不大.大家修复下小细节就可以开始整合了. Member Today’s task Next task 林豪森 协助测试及服务器部署 协助测试及服 ...

  9. 第二阶段Sprint3

    昨天:查看资料,开始视频录制部分的代码实现 今天:事实现保存到指定路径,并能够选择播放 遇到的问题:自动生成文件名,是否需要自己命名?怎么实现?

  10. 《Spring 2之站立会议3》

    <Spring 2之站立会议3> 昨天,查找了本机的端口号,并对代码作进一步的了解. 今天,对我们项目的基本框架进行了了解,即主界面和各个分界面的基本架构: 遇到的问题,虽然了解了基本框架 ...