题目链接

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. 基于Redis实现分布式锁(续)

    代码实现: redis实现分布式锁(lock:通过间隔时间段去请求Redis,来实现阻塞占用,一直到获取锁,或者超时. unlock:删除redis中key)

  2. Netty源码分析第3章(客户端接入流程)---->第3节: NioSocketChannel的创建

    Netty源码分析第三章: 客户端接入流程 第三节: NioSocketChannel的创建 回到上一小节的read()方法: public void read() { //必须是NioEventLo ...

  3. POJ 3164 Sunscreen (挑战程序设计竞赛的练习题)

    题目:https://vjudge.net/problem/POJ-3614 思路参考这个:https://blog.csdn.net/qq_25576697/article/details/7657 ...

  4. CS224n学习笔记1——深度自然语言处理

    一.什么是自然语言处理呢? 自然语言处理是计算机科学家提出的名字,本质上与计算机语言学是同义的,它跨越了计算机学.语言学以及人工智能学科. 自然语言处理是人工智能的一个分支,在计算机研究领域中,也有其 ...

  5. python 输出格式化之后的时间格式

    import timetime.strftime("%Y-%m-%d %H:%M:%S", time.localtime())

  6. Tomcat之初识初体验

    1.what's this? Stable performance, free Java web application server! 相关: Java,Javac,JVM,JRE,JDK,Java ...

  7. 获取session

    HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()) ...

  8. bing词典vs有道词典对比测试报告——功能篇之细节与用户体验

    之所以将细节与用户体验放在一起讨论,是因为两者是那么的密不可分.所谓“细节决定成败”,在细节上让用户感受方便.舒适.不费心而且温馨,多一些人文理念,多一些情怀,做出来的产品自然比其他呆板的产品更受欢迎 ...

  9. java异常处理及自定义异常的使用

    1. 异常介绍 异常机制可以提高程序的健壮性和容错性. Throwable:Throwable是java语言所有错误或异常的超类. 有两个子类Error和Exception. 1.1 编译期异常 编译 ...

  10. SDWebImage缓存图片的机制

    SDWebImage是一个很厉害的图片缓存的框架.既ASIHttp+AsyncImage之后,我一直使用AFNetworking集成的UIImageView+AFNetworking.h,但后者对于图 ...