Generate Parentheses - LeetCode
题目链接
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的更多相关文章
- N-Queens And N-Queens II [LeetCode] + Generate Parentheses[LeetCode] + 回溯法
回溯法 百度百科:回溯法(探索与回溯法)是一种选优搜索法,按选优条件向前搜索,以达到目标.但当探索到某一步时,发现原先选择并不优或达不到目标,就退回一步又一次选择,这样的走不通就退回再走的技术为回溯法 ...
- Generate Parentheses——LeetCode
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- Generate Parentheses leetcode java
题目: Given n pairs of parentheses, write a function to generate all combinations of well-formed paren ...
- [Leetcode][Python]22: Generate Parentheses
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 22: Generate Parentheseshttps://oj.leet ...
- Leetcode之回溯法专题-22. 括号生成(Generate Parentheses)
Leetcode之回溯法专题-22. 括号生成(Generate Parentheses) 给出 n 代表生成括号的对数,请你写出一个函数,使其能够生成所有可能的并且有效的括号组合. 例如,给出 n ...
- LeetCode 22. 括号生成(Generate Parentheses)
22. 括号生成 22. Generate Parentheses 题目描述 给出 n 代表生成括号的对数,请你写出一个函数,使其能够生成所有可能的并且有效的括号组合. 例如,给出 n = 3,生成结 ...
- 【题解】【排列组合】【回溯】【Leetcode】Generate Parentheses
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- 【LeetCode】22. Generate Parentheses (2 solutions)
Generate Parentheses Given n pairs of parentheses, write a function to generate all combinations of ...
- LeetCode: Generate Parentheses 解题报告
Generate ParenthesesGiven n pairs of parentheses, write a function to generate all combinations of w ...
随机推荐
- X509证书申请以及PKCS#10 详解
一.证书颁发 1.单证书的签发 1) 用户填写信息注册(或者由RA的业务操作员注册用户). 2) 用户信息传递到RA. 3) RA审核通过. 4) 用户请求发证. 5) RA审核通过. 6) 用户签发 ...
- Halcon中循环的相关算子
条件<condition> ,<condition> 内为计算成an integer or boolean value的表达式. 表达式的值1则条件为真,否则为假. 1.if( ...
- linux一切皆文件之文件描述符(一)
一.知识准备 1.在linux中,一切皆为文件,所有不同种类的类型都被抽象成文件.如:普通文件.目录.字符设备.块设备.套接字等 2.当一个文件被进程打开,就会创建一个文件描述符.这时候,文件的路径就 ...
- more和less命令详解
基础命令学习目录首页 原文链接:https://www.cnblogs.com/aijianshi/p/5750911.html 一.more命令 more功能类似 cat ,cat命令是整个文件的内 ...
- dumpe2fs命令详解
基础命令学习目录首页 dumpe2fs 显示ext2.ext3.ext4文件系统的超级快和块组信息.此命令的适用范围:RedHat.RHEL.Ubuntu.CentOS.SUSE.openSUSE ...
- 作业要求20181204-7 Final阶段第1周/共1周 Scrum立会报告+燃尽图 02
作业要求参见https://edu.cnblogs.com/campus/nenu/2018fall/homework/2481 版本控制地址https://git.coding.net/lglr20 ...
- Notes of Scrum Meeting(2014/11/2)
Notes of Scrum Meeting (2014/11/2) 软件工程项目组Sevens开始项目之后的第一次Scrum Meeting报告 会议时间:2014年11月2日 20:00—20: ...
- Daily Srum 10.30
Android那一组打算用SQL Server这个关系型数据库,而王鹿鸣他们一组却是依赖于Hbase,这是一件很麻烦的事,所以我打算在这两方面都建立一个数据库.虽然挺麻烦,但是还是为了扩展性所做的必要 ...
- Fourteenth scrum meeting
闫昊 今日完成:整理上一阶段代码,规划第二阶段实施过程 明日完成:学习讨论区开发 唐彬 今日完成:整理上一阶段代码,规划第二阶段实施过程 明日完成:学习学习进度部分开发 史烨轩 今日完成:整理上一阶段 ...
- CS小分队第二阶段冲刺站立会议(6月3日)
昨日成果:完成了主界面按钮移动交换位置 遇到问题:最后的时候发现仅交换了按钮在数据库中的信息,对于按钮的链接忘记交换了 今日计划:解决这个问题,对这个冲刺阶段的成果进行整理