题目链接

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. 在Maven上Web项目添加Spring框架

    1. pom.xml添加Spring依赖包 <!-- spring 核心依赖--> <!-- context依赖beans,aop,core,expression;core依赖log ...

  2. PPIO去中心化存储的了解和记录

    目录 介绍 FileCoin P2P技术给去中心化云存储的好处 剩余资源的再次使用 市场竞争会激发民间的智慧 PPIO的2种冗余模式 全副本模式 纠删副本模式 为什么PPIO要设计支付代理节点? 一些 ...

  3. shell--read命令

    read命令 -p(提示语句) -n(字符个数) -t(等待时间) -s(不回显) 1.基本读取read命令接收标准输入(键盘)的输入,或其他文件描述符的输入(后面在说).得到输入后,read命令将数 ...

  4. 对于新手来说,Python 中有哪些难以理解的概念?

    老手都是从新手一路过来的,提起Python中难以理解的概念,可能很多人对于Python变量赋值的机制有些疑惑,不过对于习惯于求根究底的程序员,只有深入理解了某个事物本质,掌握了它的客观规律,才能得心应 ...

  5. VSCode配合ESLint自动修复格式化

    开发Vue或者React的项目的时候,我们经常用到ESLint进行代码的校验,当代码出现不符合规范的格式的时候,ESLint会在控制台提示相关的异常信息. ESLint极大的提高了团队代码的一致性和规 ...

  6. css3 transform属性多值的顺序问题

    对于transform属性的多值的顺序问题,我自己就被困扰过.后来知道了跟顺序有关,但是不知道为什么.我想应该很多人跟我以前一样,知其然不知其所以然.如果不知道的,也许这篇文章会对大家有所帮助. 先来 ...

  7. js备忘录4

    for (var key in obj) { console.log('对象属性名:' , key); if (obj[key] instanceof Object) { sayName(obj[ke ...

  8. +new Date()的用法

    var s=+newDate();   var s=+newDate(); 解释如下:=+是不存在的; +new Date()是一个东西; +相当于.valueOf(); 看到回复补充一下.getTi ...

  9. HTML和CSS <h1> --2-- <h1>

    认识html文件基本结构 这一节中我们来学习html文件的结构:一个HTML文件是有自己固定的结构的. <html> <head>...</head> <bo ...

  10. 第一次spring冲刺第8天

    针对这几天出现的问题,我们团队做了用户需求讨论. 1.客户类型:工作者为主,其他类型都适用的计算器软件 2.需求与满足:他们想要的是能使用简单,并且适用于工作上 3.满足度:最好后台可以提供意见反馈, ...